Get the ID of a field widget in a formset - django

I want to customize the layout of forms in a formset (that is, I don't want to use .as_table() or .as_p() and the like). I'm trying to get the name of a form field for use in its label's for attribute, but I'm not sure how to go about it. I'm hoping that I won't need to construct a new name/ID for the field from scratch. Here's an example of what I'm working with right now:
{% for form in formset.forms %}
<!-- The field for the "path" form field -->
<label for="{{what do I put here?}}">{{form.fields.path.label}}:</label><input type="text" id="{{django creates this one; do I have to do my own with the for loop counter or something?}}" name="{{probably the same as id}}" />
{% endfor %}
Is there any sort of "create ID for formset field" sort of method?

This is likely what you want.
for="{{ form.your_field.html_name }}"

First, you want to use the form element's id, instead of name.
I tried Django 1.3 Alpha-1 and the following worked:
{% for form in formset.forms %}
<label for="{{ form.my_field.auto_id }}">{{ form.my_field.label }}</label>
{{ form.my_field }}
{% endfor %}
Enjoy!

Related

Get formset name variables in django templates

I am sending a formset to my template. I want to add some style to it, so I need to access his variables.
I took the following code from documentation, where they print the form and it has some attributes, amongside them the name="form-0-title".
for form in formset:
print(form.as_table())
<tr><th><label for="id_form-0-title">Title:</label></th><td><input type="text" name="form-0-title" value="Django is now open source" id="id_form-0-title"></td></tr>
...
As I want to style my forms, I need to give the inputs a name to know how to treat them in the backend. When I do the following in the templates
{{ proficiency_form.management_form }}
{% for form in proficiency_form %}
{{form.title.name}}
{% endfor %}
I get title, title, ... instead of id_form-0-title, id_form-1-title, ...
How can I get the correct name in the templates?
You can easily get the input's name of your fields like so:
{{ proficiency_form.management_form }}
{% for form in proficiency_form %}
{{form.title.html_name}}
{% endfor %}
If you want to get the label's id, you can consider this approach:
{{ proficiency_form.management_form }}
{% for form in proficiency_form %}
{{form.title.id_for_label}}
{% endfor %}
Read more about it in the official Docs.
You can find a similar(almost duplicate) question here.

Render form without tags django

{% for fields in form %}
{{ field.label }}
{{ field }}
{% endfor %}
The {{ field }} will render something similar to:
<input type="text" id="something_id" .../>
Is there a way I can render a field without having to let it wrap it in a tag? So for instance, I render a field and I get (without the input tag):
type='text' id='_something_id'
In this case, I can go ahead to manually add custom classes as and when and where I want like:
<input {{ field }} class='class class-again' required autocomplete='off' />
Is there a way I can do that in Django, render a field without it wrapped in the element?
Obviously, such a hard way of formatting can be tiring if working on tons of fields making such an approach impractical. In my case, I'm just wanting to tailor some css to just two fields, and having to go through Django Forms, or template tags etc, I find the process lengthy
You can do this if you know the input type:
{% for field in form %}
{{ field.label_tag }}
<input type="text" id="{{ field.id_for_label }}" name="{{ field.html_name }}" value="{{ field.value }}"/>
{% endfor %}
If you have different input types you could still go through the fields manually instead of iterating over them (using their name, eg. form.username).
However in most cases it is better to do those things on the backend using the widget API like this:
name = forms.CharField(widget=forms.TextInput(attrs={'class': 'special'}))
It is possible. You could write your own HTML code, you know that Django assigns id and name attributes with a format, like: id='something_id'. The name attribute is the important one, you should make sure it is the same that Django would assigns.
Another option is this:
# I will assume you have a field named something
something = forms.CharField(widget=forms.TextInput(attrs={'class':'class class-again''}))
This should do what you need. I hope this helps.

How to get label tag in html from django form

{% for field in columnMetaData %}
{{field}}
{% endfor %}
Code above only displays <input>...</input>
But how to get and <label></label>, because labels exists in columnMetaData
I forgot to say that columnMetaData is ModelForm
NEW UPDATE
Just another question. Is there way to add class in each {{field}} ?
And field to be like this: <input type="" class="something" ></input>
You could use attrs to set classes for your field. Usage: field=forms.CharField(attrs={'class': 'some-class'})
Like that :
{% for field in columnMetaData %}
{{ field.name.label }}
{% endfor %}

Setting ID for Radio buttons in Django

It is great that Django 1.4 allows fine graining of radio select
{% for radio in form.important_client reversed%}
{{radio.tag}}<label for="????">{{radio.choice_label}}</label>
{% endfor %}
but for some odd reason when using this methodology, the <input> have no IDs. And hence I can't set the <label for='ID' /> accordingly. That causes big issues in my CSS.
Is there anyway to get the IDs set nonetheless?
While debuging a RadioSelect rendering, I got no idea of using radio tag and label elegantly. So here is my attempt to solve your problem:
{% for radio in form.important_client reversed %}
<input name="{{ radio.name }}" type="radio" id="radio_{{ radio.index }}" value={{ radio.choice_value }}>
<label for="radio_{{ radio.index }}">{{ radio.choice_label }}</label>
{% endfor %}
Instead of radio.index property, which is not documented, you can use forloop.counter.
Just in case I attach a screenshot of debug window, where example of radio context is shown (form_of_field variable on a figure):
This is one way to do that, it might not be the best but it works. In your form you can set the id for each of the choices like this:
from django import forms
class MyForm(forms.Form):
CHOICES = (('1','Available'),('2','Not Available'))
input = forms.ChoiceField(widget=RadioSelect(attrs={'id' : 'myId'},choices=CHOICES)
Then on your template:
{% for radio in form.input %}
{{ radio }}
{% endfor %}
And your HTML will look like this:
<label for="myId_0"><input id="myId_0" name="input" type="radio" value="1"></label> Available
<label for="myId_0"><input id="myId_0" name="input" type="radio" value="2"></label> Not Available
I hope this works!

How I can get object attributes for different language codes in Django template?

I am using django-transmeta for translation. In the below code, {{ obj.description }} returns the description in the current language of django. What I need is, getting the obj.description_[lang_code]. How can I get it?
{% for lang in languages.all %}
<div id='{{ lang.code }}'>
<input type="text" name="description-{{lang.code}}" value='{{ obj.description }}'/>
</div>
{% endfor %}
As I understood from your comment you want to get description of specific language in for loop?
then simply write a custom filter like in this way
{{ obj|get_lang_info:lang.code }}
here get_lang_info is custom filter.