django-templates

How do I get all the variables defined in a Django template?

风格不统一 提交于 2019-12-04 07:44:19
问题 I'm new to Django and I wonder if there is a way to dump all the variables available to a template for debugging purposes. In Python I might use something like locals() , is there something equivalent for the default template engine? Note: suppose I don't have access to the view for the purposes of this question. 回答1: Both Ned's and blaine's answers are good, but if you really want to achieve exactly what you ask for there's a template tag for it: {% debug %} Builtins:debug More information

Converting a nested dictionary to a list

我们两清 提交于 2019-12-04 07:19:10
I know there are many dict to list questions on here but I can't quite find the information I need for my situation so I'm asking a new quetion. Some background: I'm using a hierarchical package for my models and the built-in function which generates the tree structure outputs a nested loop to indicate parents, children, etc. My goal is to keep the logic in views and output a list so that I can simply loop over it in my templates. Here is my data, in the tree structure: 1 -1.1 --1.1.1 ---1.1.1.1 --1.1.2 -1.2 --1.2.1 --1.2.2 -1.3 Here is the nested dictionary I am getting as a result { <Part: 1

MultiValueDictKeyError generated in Django after POST request on login page

戏子无情 提交于 2019-12-04 06:23:43
I'm trying to build a login page. I'm running Django 1.6.1. I've largely been following the tutorial at www.fir3net.com/Django/django.html. For convenience I will repost a lot of it here. Error Message: Request Method: GET Request URL: http://127.0.0.1:8000/login/ Database In Use: SQLite3 Django Version: 1.6.1 Python Version: 2.7.4 Installed Applications: ('django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'django.contrib.contenttypes', 'bookmarks') Installed Middleware: ('django

Django Template Language - getting url encoded values

允我心安 提交于 2019-12-04 05:43:14
问题 I'm using Django Template language, using load staticfiles . However when I do the following <img src="{% static 'img/category/{{category.category|lower|slugify}}.jpg' %}"> I get the HTML as <img src="/static/img/category/%7B%7Bcategory.category%7Clower%7Cslugify%7D%7D.jpg"> which obviously is not rendering the correct image. However my expected output was <img src="/static/img/category/electronics.jpg"> where category.category = electronics I'm passing category as a ctx variable. Why is it

Django add class to form <input ..> field

混江龙づ霸主 提交于 2019-12-04 05:35:38
We are looking for a solution to add a CSS class attribute to a Django form's <input..> field. We've seen the advice that suggests we wrap the field in a <div> http://docs.djangoproject.com/en/1.2/topics/forms/#customizing-the-form-template , but this advice mainly seems to apply to a fields label, not it's <input ...> . This particular advice doesn't work if you are trying to create a border around the <input> field. In that case, the <div> will be applied to the bounding box, and not the actual input field. For example .wrapper { border: 1px solid #666;line-height:22px;height:22px;padding

Django Admin: using different templates for two admin site

社会主义新天地 提交于 2019-12-04 05:19:11
I've a Django project with two different admin-site (as described in documentation ) I would like to have different custom template for each of them. I know how to override custom template, by putting html files in myproject/templates/admin/ directory. However, both admin-site use those templates ! I don't understand how to specify another set of custom templates. Ideally, I would like having: # For first admin site myproject/templates/admin-a/ base.html base_site.html and: # For second admin site myproject/templates/admin-b/ base.html base_site.html Any ideas ? Thanks, Stéphane first option

How to render the radio buttons without ul in django

白昼怎懂夜的黑 提交于 2019-12-04 05:09:09
forms.py Date_Format = ( ('0', ' dd / mm / yyyy'), ('1', 'mm / dd / yyyy'), ) Time_Format = ( ('0', ' 12 hour AM / PM '), ('1', ' 24 hour '), ) class SettingsForm(forms.ModelForm): date_format = forms.ChoiceField(widget=forms.RadioSelect(), choices=Date_Format) time_format = forms.ChoiceField(widget=forms.RadioSelect(), choices=Time_Format) template.py Update: {% for radio in SettingsForm.date_format %} {{ radio.choice_label }} <div class="select">{{ radio.tag }}</div> {% endfor %} The above form is rendering the radio buttons in a unordered list, but I want to be with out unordered list. How

Django: how to get the name of the template being rendered

半腔热情 提交于 2019-12-04 05:06:48
I'm implementing a bootstrap navbar as show in this example here Items in a navbar are <li>'s , the "selected" item has the attribute class="active" : <li class="active"> <a href="#"> Link1 </a> </li> <li> <a href="#"> Link2 </a> </li> In Django these items will be within a template, which gets included by any templates that are supposed to display the navbar. I'm thinking about doing it this way: <li> <a href="/" class="{% if template_name == "home.djhtml" %}active{% endif %}"> Home </a> </li> <li> <a href="about/" class="{% if template_name == "about.djhtml" %}active{% endif %}"> About </a>

Django TemplateDoesNotExist admin/login.html

回眸只為那壹抹淺笑 提交于 2019-12-04 05:06:42
问题 I am working with django 1.4. And this error appeared: TemplateDoesNotExist at /admin/ admin/login.html I tried to reinstall django, but i did not work... Please Help! 回答1: You can try to add the admin tempates path name to TEMPLATES['DIRS'] in your django settings.py The default TEMPLATES: 'DIRS': [os.path.join(BASE_DIR, 'templates')] change to if you are using Django project created by Pycharm: 'DIRS': [os.path.join(BASE_DIR, 'templates'), os.path.join(BASE_DIR, 'venv/lib/site-packages

How to parse django style template tags

若如初见. 提交于 2019-12-04 05:03:17
问题 What's the best way to parse django style template tags in PHP? I know there are plenty of templating libraries for PHP, but I literally just need to parse one tag when I retrieve data from my database. The style of tag I want to parse looks like {{ form | form_name }} 回答1: REGEX is the way to go. \{\{ *form *\| *(\S+) *\}\} should do the trick nicely. A bit of explaining: \{ and \} will each match { and } respectively. * will match for 0 to infinite space characters ( ). (\S+) will match for