django custom form template checkbox question - django

Django forms are really easy and nice to style especially if you like to take control of the outcome.
A question. Is there anyway you can see whether the {{ field }} type, ie checkbox, radio etc?

you could create a field_type template filter
{{ field|field_type }} = CharField
{{ field|widget_type }} = TextInput
heres a great example :
http://olivergeorge.posterous.com/django-template-tags-to-find-out-field-type

Related

Force Django to recognize a model field value as a template tag value?

I am allowing users to create custom templates, and when doing so they are building off of my own base template with context variables.
So my own template that is used as a base will have a line such as..
<h3>1.0: Contract Specifics for {{ first_name }}</h3>
Now when the user edits this form I am escaping the curly brackets so they store as actual curly brackets in the form field when the user saves their custom template.
Now a user will click save and in the model field there will be raw HTML stored as a TextField and is stored in the database as such...
Now I need to render this in my django template later on but, I need {{ first_name }} to be replaced the actual context variable. unfortunately in my django template when I render this specific form field to display the HTML
{% autoescape off %}
{{ contract_detail }}
{% endautoescape %}
it displays as..
1.0: Contract specifics for {{first_name}}
Is there anyway to force django to recognize this as a template variable and render this correctly, maybe I need some sort of custom functionality, or templatetag?

Django UpdatView - custom form fields

I have my UpdateView for updating some of my data.
class WpisUpdate(UpdateView):
model=ProdukcjaStanTb
fields=[
'temat',
'podtemat',
'proc_wym',
'proc_auto',
'proc_palnik',
'proc_uruch',
'proc_pakowanie',
]
Now, in my template I'd like to have only :
'proc_wym',
'proc_auto
'proc_palnik',
'proc_uruch',
'proc_pakowanie',
Fields, but also have access to fields "temat" and "podtemat" (to make big titles or web page title for example). In template i'm using {{form.temat.value}} tags, which are ok, but requires those fields in field list in UpdateView. I don't want user to change that. Is there any quick way to have those fields hidden in form but accessible while using easy:
{{ form.as_p }}
in template ? Or do i have to manually edit my form and add some html attributes, like read-only or input type="hidden" ?
Since this view only updates object you can always exclude unnecessary fields from autogenerated modelform - simply del them from your fields declaration then access 'temat', 'podtemat' in your template using object
template
{{ object.temat }} {{ object.podtemat }} {{ form.as_p }}
view
class WpisUpdate(UpdateView):
model = ProdukcjaStanTb
fields=[
'proc_wym',
'proc_auto',
'proc_palnik',
'proc_uruch',
'proc_pakowanie',
]

Django ForeignKey form field: display value

I might just be having a brain fart, but I seriously can't figure this out. How do I get display the values of ForeignKey fields from a bound form in a template? {{ field }} renders a widget, {{ field.value }} returns the pk, and I need the model itself.
I am writing a custom template for django-crispy-forms; which is why I only get a form field in my context, but not a model field.
You must try this: {{ form.instance.field }}

How to get list of multiple choices

I have a problem rendering a template variable for example {{ profile.speciality }}
that resulted in [u'pediatrics'] on the web page, what I actually wanted is simply
pediatrics. The data is coming from MultipleChoiceField and a CheckboxSelectMultiple
widget, because user should be able to select multiple options.
Then i used request.POST.getlist('speciality') to populate data in to the model instance,
for example:
user_profile = UserProfile(speciality=request.POST.getlist('speciality'))
I also tried to iterate over {{ profile.speciality }} in the template but what I am getting is string iteration rather than a list iteration.
I am an absolute beginner, I have no programming experience, pardon me if I did anything stupid, need help desperately, tried everything I could.
thank you
You can use this to render a MultipleChoiceField
{% for speciality in profile.speciality.all %}
{{ speciality }}
{% endfor %}

Django template can't access a form field if it is inside an array

Fairly new to Django here, so I don't know if I'm just not getting it or this is a bug. Let's say I have a form class:
class SurveyTwo(forms.Form):
food = [forms.BooleanField(required=False, initial=False, label="Seafood")]
Then, in the corresponding template, I am trying to access this by typing
{{ form.food.0 }}
When I do this, I get on my page:
<django.forms.fields.BooleanField object at 0x1c5b990>
Not the "Seafood" checkbox I was looking for. I can access the label just fine by doing {{ form.food.0.label }} but the checkbox just appears as that string. Should I be able to do this or not?
Essentially what I am trying to do is to pass an array of checkboxes to my form template, rather than having to define each form variable/field. I want to do this because I'm going to have a large number of checkboxes and want to be able to lay them out in a certain order (with a 2D array), rather than define them and lay them all out manually. If I can't do the above, does anyone know of a simpler solution? Thanks.
Mark
You can register simple template tag:
from django import template
register = template.Library()
#register.simple_tag
def bound_field(form, name):
""" returns bound field """
return form.__getitem__(name)
Then in template you just use:
{% bound_field form <field_name> %}
where is name of field.
If you have dynamicly generated fields that names you don't know you can access to them via fields.keys in this case generating all fields will look like
{% for name in form.fields.keys %}
{% bound_field form name %}
{% endfor %}