Web Development with Django Cookbook by 2014

Web Development with Django Cookbook by 2014

Author:2014
Language: eng
Format: epub, mobi
Publisher: Packt Publishing


Getting ready

Once again, we will start with the utils app that should be installed and ready for custom template tags.

How to do it...

Template tags consist of function parsing arguments passed to the tag and a node class that renders the output of the tag or modifies the template context. Perform the following steps:

First, let's create the function parsing the template-tag arguments, as follows: #utils/templatetags/utility_tags.py # -*- coding: UTF-8 -*- from django.db import models from django import template register = template.Library() ### TAGS ### @register.tag def get_objects(parser, token): """ Gets a queryset of objects of the model specified by app and model names Usage: {% get_objects [<manager>.]<method> from <app_name>.<model_name> [limit <amount>] as <var_name> %} Example: {% get_objects latest_published from people.Person limit 3 as people %} {% get_objects site_objects.all from news.Article limit 3 as articles %} {% get_objects site_objects.all from news.Article as articles %} """ amount = None try: tag_name, manager_method, str_from, appmodel, str_limit,\ amount, str_as, var_name = token.split_contents() except ValueError: try: tag_name, manager_method, str_from, appmodel, str_as,\ var_name = token.split_contents() except ValueError: raise template.TemplateSyntaxError, \ "get_objects tag requires a following syntax: " "{% get_objects [<manager>.]<method> from "\ "<app_ name>.<model_name>"\ " [limit <amount>] as <var_name> %}" try: app_name, model_name = appmodel.split(".") except ValueError: raise template.TemplateSyntaxError, \ "get_objects tag requires application name and "\ "model name separated by a dot" model = models.get_model(app_name, model_name) return ObjectsNode(model, manager_method, amount, var_name)



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Popular ebooks
Whisky: Malt Whiskies of Scotland (Collins Little Books) by dominic roskrow(56049)
What's Done in Darkness by Kayla Perrin(26607)
The Fifty Shades Trilogy & Grey by E L James(19089)
Shot Through the Heart: DI Grace Fisher 2 by Isabelle Grey(19073)
Shot Through the Heart by Mercy Celeste(18946)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 10 by Isuna Hasekura and Jyuu Ayakura(17125)
Python GUI Applications using PyQt5 : The hands-on guide to build apps with Python by Verdugo Leire(17007)
Peren F. Statistics for Business and Economics...Essential Formulas 3ed 2025 by Unknown(16886)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 03 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16835)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 01 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16459)
The Subtle Art of Not Giving a F*ck by Mark Manson(14368)
The 3rd Cycle of the Betrayed Series Collection: Extremely Controversial Historical Thrillers (Betrayed Series Boxed set) by McCray Carolyn(14151)
Stepbrother Stories 2 - 21 Taboo Story Collection (Brother Sister Stepbrother Stepsister Taboo Pseudo Incest Family Virgin Creampie Pregnant Forced Pregnancy Breeding) by Roxi Harding(13660)
Scorched Earth by Nick Kyme(12779)
Drei Generationen auf dem Jakobsweg by Stein Pia(10973)
Suna by Ziefle Pia(10896)
Scythe by Neal Shusterman(10355)
The Ultimate Python Exercise Book: 700 Practical Exercises for Beginners with Quiz Questions by Copy(10011)
D:\Jan\FTP\HOL\Work\Alien Breed - Tower Assault CD32 Alien Breed II - The Horror Continues Manual 1.jpg by PDFCreator(9984)
De Souza H. Master the Age of Artificial Intelligences. The Basic Guide...2024 by Unknown(9964)