python - How to set a custom attribute to a model which can be used in the template with ModelForm in Django? -
is possible write attributes model field can used later differentiate different fields in template?
model.py
from django.db import models class person(models.model): first_name = models.charfield("i label", max_length=30) last_name = models.charfield("i other label", max_length=30, customattr="custom")
forms.py
class personform(modelform): class meta: person
template.html
<form action="" method="post">{% csrf_token %} {% field in form %} {% ifequal field.customattr 'custom' %} # how work? <p>hello world.</p> {{ field }} {% else %} <p>this not custom</p> {{ field }} {% endifequal %} {% endfor %} <input type="submit" value="submit" /> </form>
any hints?
not possible; field
in template code form field, not model field. shift presentation logic model template, , this:
<form action="" method="post">{% csrf_token %} {% field in form %} {% if field.name == 'last_name' or field.name == 'another_field' %} <p>hello world.</p> {{ field }} {% else %} <p>this not custom</p> {{ field }} {% endif %} {% endfor %} <input type="submit" value="submit" /> </form>
(the == operator added in django 1.2)
Comments
Post a Comment