django-templates

Django Templates First element of a List

橙三吉。 提交于 2019-12-03 04:38:00
I pass a dictionary to my Django Template, Dictionary & Template is like this - lists[listid] = {'name': l.listname, 'docs': l.userdocs.order_by('-id')} {% for k, v in lists.items %} <ul><li>Count: {{ v.docs.count }}, First: {{ v.docs|first }}</li></ul> {% endfor %} Now docs is a list of userdocs type. i.e. is an instance. So first filter returns me this instance. From this I need to extract it's id . How do I do that? I tried {{ v.docs|first }}.id and various other futile trials. You can use the {% with %} templatetag for this sort of thing. {% with v.docs|first as first_doc %}{{ first_doc.id

In Django, is it possible to access the current user session from within a custom tag?

 ̄綄美尐妖づ 提交于 2019-12-03 04:34:55
I am writing a custom tag in Django that should output a value stored in a user session, but I cannot find a way to access the session object from within a custom tag function. Is there any way to do this, without manually assigning the session object to a context variable? Michael Warkentin You should be able to add the request context processor in your settings.py file: TEMPLATE_CONTEXT_PROCESSORS = ("django.core.context_processors.auth", "django.core.context_processors.debug", "django.core.context_processors.i18n", "django.core.context_processors.media", 'django.core.context_processors

How can I change the way a boolean prints in a django template?

让人想犯罪 __ 提交于 2019-12-03 04:24:23
I have some django code that prints a BooleanField it is rendered as True or False, can I change the label to be Agree/Disagree or do I need to write logic for that in the template? {{ bool_var|yesno:"Agree,Disagree" }} You can also provide an additional string for the None case. See the docs for yesno for details. 来源: https://stackoverflow.com/questions/845901/how-can-i-change-the-way-a-boolean-prints-in-a-django-template

Django: Hide button in template, if user is not super-user

只谈情不闲聊 提交于 2019-12-03 04:13:37
问题 How do you get your template/view to recognize whether or not a logged in user is a super user or not? There are certain buttons on my forms (in the template) that I want completely hidden if the user is not a super-user How would you go about doing that? 回答1: Check out is_superuser on the User object: {% if request.user.is_superuser %} ... <button>...</button> ... {% else %} ... {% endif %} EDIT: after @mustafa-0x comments The above assumes that you have django.core.context_processors

How to render an ordered dictionary in django templates?

大憨熊 提交于 2019-12-03 04:06:46
问题 I'm trying to learning django templates but it's not easy. I have a certain views.py containing a dictionary to be rendered with a template. The dictionary is made of key-value pairs, where key are unique names and values are some values associated to those names. I render the dictionary in the following way: return render_to_response('results.html', {'data': results_dict}) Now my problem is that in my template I need to display the names in alphabetical (or ASCIIbetical) order with the

Error when using django.template

大憨熊 提交于 2019-12-03 03:49:22
问题 I'm a beginner in django and I got a lot of errors when using template module from django. The following works from the python shell: from django import template t = template.Template('My name is {{ name }}.') When i use this code , i get the following error: Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python2.7/site-packages/django/template/base.py", line 123, in __init__ if settings.TEMPLATE_DEBUG and origin is None: File "/usr/lib/python2.7/site

Django — How to have a project wide templatetags shared among all my apps in that project

风格不统一 提交于 2019-12-03 03:37:56
问题 Second time asking more details ... I'd like to have a project wide templagetags directory to have the common tags used by all Apps, then each app can have their own tags if need so. Let say that I have: proj1/app1 proj1/app1/templatetags/app1_tags.py proj1/app2 proj1/app2/templatetags/app2_tags.py proj1/templatetags/proj1_tags.py proj1/templates/app1/base.html proj1/templates/app1/index.html proj1/templates/app2/base.html proj1/templates/app2/index.html Where: proj1/templates/app1/base.html

Nested django templates

有些话、适合烂在心里 提交于 2019-12-03 03:05:52
This seems like a pretty basic thing to do but although I've been using Django for around a year, I've never run into this scenario yet. In a lot of templating/web frameworks, template inheritance works a bit differently, in that usually it behaves more like wrappers, so if you have childtemplate.html, parenttemplate.html, and grandparenttemplate.html, then the finally rendering usually looks something like: grandparent header parent header child header child content parent content parent footer grandparent content grandparent footer That's not exactly how it works in Django but I'm wondering

Comma separated lists in django templates

谁都会走 提交于 2019-12-03 02:54:23
问题 If fruits is the list ['apples', 'oranges', 'pears'] , is there a quick way using django template tags to produce "apples, oranges, and pears"? I know it's not difficult to do this using a loop and {% if counter.last %} statements, but because I'm going to use this repeatedly I think I'm going to have to learn how to write custom tags filters, and I don't want to reinvent the wheel if it's already been done. As an extension, my attempts to drop the Oxford Comma (ie return "apples, oranges and

Django form field grouping

淺唱寂寞╮ 提交于 2019-12-03 02:52:22
Say I have a form with 20 fields, and I want to put 10 of these fields (group1) in a particular div environment and the other 10 fields (group2) in a different div environment. Something like: <div class="class1"> {% for field in form.group1 %} {{ field.label}}: {{ field }} {% endfor %} </div> <div class="class2"> {% for field in form.group2 %} {{ field.label}}: {{ field }} {% endfor %} </div> Any ideas how I could accomplish this by iterating over the fields? More generally, I would like to be able to do this with many different div environments and sets of form fields. Any logical way to