django-templates

django form: how to check out checkbox state in template

随声附和 提交于 2019-12-03 17:07:54
I have a form with checkboxes that works fine, but when the user submits the form with errors and my checkbox is checked I need to change div class which holds the checkbox. So I was experimenting with {{ form.mycheckbox.data }} in the template and it says False on page load. Now when user is click on a checkbox and the form has errors, it says on . So I tried: {{ if form.mycheckbox.data == True }} doesn't work {{ if form.mycheckbox.data != False }} doesn't work {{ if form.mycheckbox.data == 'on' }} doesn't work {{ if form.mycheckbox.data == on }} doesn't work Use {% if form.mycheckbox.value %

Django template object type

南笙酒味 提交于 2019-12-03 16:46:24
问题 Alright, here's my situation. I've got an array of generic objects that I'm iterating over in a django template. Those objects have a number of subclasses and I want to figure out in the template which subclass I'm dealing with. Is this possible? Advisable? The code might look something along the lines of (where the if statements include some imaginary syntax): <table> <tr> <th>name</th> <th>home</th> </tr> {% for beer in fridge %} <tr> <td> {{ beer.name }} </td> <td> {% if beer is instance

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

我与影子孤独终老i 提交于 2019-12-03 16:07:13
问题 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? 回答1: 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

Django form multiple select box size in template

人走茶凉 提交于 2019-12-03 15:58:52
I have a template: ... <form action="/reportform/" method="post"> <p><label>Aircraft system:</label> <br>{{ Querry.system }} ... it looks like this How can I set a Size option for this box? for example, 10. Use the attrs attribute to define the size. class MyForm(forms.Form): system = forms.ChoiceField(choices=SYSTEM_CHOICES, widget=forms.SelectMultiple(attrs={'size':'40'})) Sometimes, it is useful to override the widget in the forms init method. class MyForm(forms.Form): <snip> def __init__(self, *args, **kwargs): super(MyForm, self).__init__(*args, **kwargs) self.fields['system'].widget =

Django, templates, for loops, and cycles

﹥>﹥吖頭↗ 提交于 2019-12-03 15:13:46
(tl;dr at the bottom) Let me try to explain what I'm trying to accomplish: I have a two dimensional array, and I would like to display its contents a certain way. I want "rows", and each row can display no more than three "objects", for lack of a better word. So I want to iterate over the array and create my HTML in the process. My idea is this: every "first of three" elements in the array should open up the "row". Every "third of three" elements should close the "row". However, if the last element in the [inner] array does not happen to be the "third of three", it should still close the row.

Django - How to add html attributes to forms on templates

给你一囗甜甜゛ 提交于 2019-12-03 14:59:39
Suppose I have the following template: <div id="openid_input_area"> {{ form.openid_identifier }} <input name="bsignin" type="submit" value="{% trans "Sign in" %}"> </div> How can I add a css class to form.openid_identifier? As I am adhering to the SOC principles and I expect designers to improve the templates I do NOT want to do this on the form model but on the template itself. I couldn't find anything on this on the docs. Is there a way to do this on the template? I've managed to code a simple solution to my problem: I wrote a custom template tag : from django import template register =

Tell django to search app's template subfolders

喜你入骨 提交于 2019-12-03 14:43:34
I have the following folder structure for the templates on my django app: templates/ app/ model1/ model1_form.html model2/ model2_form.html Suppose I'm using model1 and a generic ListView, right now it only searches at templates/app/model1_form.html. Is there anyway I can tell django he should also search the app/ subfolders? I don't want to have to set the template name and path manually ( template_name="templates/app/model1/model1_form.html" ). At settings.py I have: import os.path BASE_PATH = os.path.dirname(os.path.dirname(__file__)) TEMPLATE_DIRS = ( BASE_PATH+'/templates/', ) This is my

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

吃可爱长大的小学妹 提交于 2019-12-03 14:37:32
问题 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? 回答1: {{ 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 Admin Customizing

强颜欢笑 提交于 2019-12-03 14:15:07
问题 I am designing an admin interface where invite mails will be sent to users. My Invitation model is ready & in my invitation admin interface I am able to see my added users for which the admin can send email invites. now I want to customize this a bit. I want to add for each row a SEND button which will actually send an email to that user. Sending email function etc. are all ready. I am not getting as to how I can customize this admin template to add a send button. Can someone help ?? or

TypeError object is not iterable

廉价感情. 提交于 2019-12-03 13:41:29
I am getting the following error when trying to loop over a variable in my Django templates. The variable in question is the related object of the model specified in my DetailView subclass: TypeError at /en/applicants/50771459778/ 'Householdmember' object is not iterable Here is my models.py file: class Applicant(models.Model): user = models.ForeignKey(User, editable=False) bank_card_number = models.CharField(_('Bank card number'),max_length=50, unique=True) site_of_interview = models.IntegerField(_('Site of interview'), choices = SITE_CHOICES, default=TIRANA, blank=False) housenumber = models