django-templates

Django: Built-in include tag vs custom inclusion tag

六眼飞鱼酱① 提交于 2019-12-03 11:36:04
What is the difference between Django's built-in include tag and custom inclusion tags ? I have read the documentation, and both seem to to achieve the same goal: render a template passing it a context or variable. They serve different purposes. The include tag simply includes the content from an existing template in its entirety and unmodified. A custom inclusion tag passes the context to a function which can contain logic to manipulate the context before passing it to a template. For example, perhaps I have a panel that will be shown on multiple pages. The panel's template requires a few

Render one queryset into 2 div columns (django template)

↘锁芯ラ 提交于 2019-12-03 11:34:19
问题 Is there a good way to render the enumeration of a queryset into two div columns? Using 960 grid, I've got something to the effect of... <div class="container_16"> <div class="grid_8 alpha"></div> <div class="grid_8 omega"></div> </div> In Django, one model needs to have it's enumerated contents rendered in both of those columns, and preferably somewhat equally. For the moment, I've got some ugly code that in the view splits the QuerySet into 2 halves, and then each half is rendered in their

Infinite recursion while extending the admin's app change_form template

让人想犯罪 __ 提交于 2019-12-03 11:22:54
I have the following template in template/admin/change_form.html : {% extends "admin/change_form.html" %} {% block extrahead %} {% include "dojango/base.html" %} {% block dojango_content %} {% endblock %} {% endblock %} However for some reason it throws a TemplatesyntaxError: TemplateSyntaxError at /admin/cms/post/add/ Caught RuntimeError while rendering: maximum recursion depth exceeded while calling a Python object I know it's late, but... If extending - which is a far better option than duplicating - the key is to have it named anything except /admin/change_form.html . (Although the OP

Django template counter in nested loops

流过昼夜 提交于 2019-12-03 11:17:21
Hi I have a list of two dictionaries I am passing to a Django template: base_parts = [ {'important item': 43}, {'lesser item': 22, 'lesser item': 3, 'lesser item': 45} ] in my template I can do this: {% for base_part in base_parts %} {% for k, v in base_part.items %} {# ...do stuff #} {# I try to get a running total of items to use as an ID #} inner ID: {% forloop.counter0 %}< br/> outer ID: {% forloop.parentloop.counter0 %}< br/> {% endfor %} {% endfor %} As you can see, what I want is a running total of the total number of items I have iterated through, but both methods I have included

Django: How to access the display value of a ChoiceField in template given the actual value and the choices?

谁说我不能喝 提交于 2019-12-03 10:56:18
I have a ChoiceField in a bound form, whose choices are: [('all', 'All users'), ('group', 'Selected groups'), ('none', 'None')] In the template, I have no problem accessing its bound value (the actual value to be stored; the first element of each tuple) and the choices. With these pieces of info in hands, I know I should be able to access the display values, or the second element of each tuple. But how can I do that in the template? Thanks. bmihelac I doubt that it can be done without custom template tag or filter. Custom template filter could look: @register.filter def selected_choice(form,

Django Newbie : “Reverse not found”

巧了我就是萌 提交于 2019-12-03 10:54:58
问题 I have a line in a Django form : {% for aa in my_array %} which seems to be triggering this error : Template error Caught an exception while rendering: Reverse for 'dev_env.profiles.views.viewPlan' with arguments '('',)' and keyword arguments '{}' not found. What does this error message really mean? I suspect that either the line is correct, but the error message is wrong. Or the error is real but this line is a red-herring. What on earth should I be looking for? Update : Paulo sorted this,

Add class to form field Django ModelForm

别等时光非礼了梦想. 提交于 2019-12-03 10:54:38
I am trying to write a Bootstrap Form with Django ModelForm. I have read the Django Documentation Django Documentation about Forms , so I have this code: <div class="form-group"> {{ form.subject.errors }} <label for="{{ form.subject.id_for_label }}">Email subject:</label> {{ form.subject }}</div> The {{form.subject}} is rendered by Django, for example in CharField field model, as input tag, <input type="text"....> etc. I need add "form-control" class to every input in order to get Bootstrap input appearance (without third-party packages). I found this solution Django add class to form field .

Obtain the first part of an URL from Django template

孤街浪徒 提交于 2019-12-03 10:45:28
I use request.path to obtain the current URL. For example if the current URL is "/test/foo/baz" I want to know if it starts with a string sequence, let's say /test. If I try to use: {% if request.path.startswith('/test') %} Test {% endif %} I get an error saying that it could not parse the remainder of the expression: Could not parse the remainder: '('/test')' from 'request.path.startswith('/test')' Request Method: GET Request URL: http://localhost:8021/test/foo/baz/ Exception Type: TemplateSyntaxError Exception Value: Could not parse the remainder: '('/test')' from 'request.path.startswith('

how to check DEBUG true/false in django template - exactly in layout.html [duplicate]

笑着哭i 提交于 2019-12-03 10:32:13
问题 This question already has answers here : How to check the TEMPLATE_DEBUG flag in a django template? (6 answers) Closed 4 years ago . I would like distinguish a look of some toolbar in layout.html depending if DEBUG = True or not. I am aware of this answer using django.core.context_processors.debug but it forces me to use RequestContext instead of Request what I not really like, btw how can I use RequestContext for layout.html which extends base.html ? And generally is there some better way to

Configuring django settings to work with 1.4.1. Loading template error

北战南征 提交于 2019-12-03 10:26:56
Here is the error I got: ImproperlyConfigured: Error importing template source loader django.template.loaders.filesystem.load_template_source: "'module' object has no attribute 'load_template_source'" Here is my loader template code: if DEBUG: TEMPLATE_LOADERS = [ 'django.template.loaders.filesystem.Loader', 'django.template.loaders.app_directories.Loader', ] else: TEMPLATE_LOADERS = [ ('django.template.loaders.cached.Loader',( 'django.template.loaders.filesystem.load_template_source', 'django.template.loaders.app_directories.load_template_source', 'forum.modules.template_loader.module