I have a very simple ModelForm in my app that looks like this:
# ModelForm
class ProductForm(ModelForm):
class Meta:
model = MyModel
exclude = ['created', 'last_modified', 'serial_number']
# Model
class BaseModel(models.Model):
created = models.DateTimeField(auto_now_add=True, blank=True, null=True)
last_modified = models.DateTimeField(auto_now=True, blank=True, null=True)
class MyModel(BaseModel):
product = models.TextField(verbose_name='Product Name')
serial_number = models.TextField(verbose_name='Serial Number')
And a form that looks like this:
# Form
<form method="POST" action="{% url some_url %}">
{% csrf_token %}
{{ formset.management_form }}
{% for form in formset %}
{% for field in form %}
{% if field.errors %}
<div>{{ field.errors }}</div>
{% endif %}
<div>
{{ field.label_tag }}:
{{ field }}
</div>
{% endfor %}
{% endfor %}
<div class="actions">
<input class="button submit focus" type="submit" value="{% trans "Save" %}" />
</div>
</form>
When I check out the view using this, I just see a colon (:) followed by the textfield: The label is gone.
According to the documentation for ModelForm:
In addition, each generated form field has attributes set as follows:
...
The form field’s label is set to the verbose_name of the model field, with the first character capitalized.
What mistake have I made?
I am using Django 1.4.1 if it matters.
You have to put the field label inside a <label> tag. So:
<div>
<label for="id_{{field.html_name}}">{{field.label}}:</label>
{{ field }}
</div>
The only solution I managed to find that still allowed me to separate each row of the form was doing the following:
<form method="POST" action="{% url some_url %}">
{% csrf_token %}
{{ formset.as_ul }}
<div class="actions">
<input class="button submit focus" type="submit" value="{% trans "Save" %}" />
</div>
</form>
... the key piece being the {{ formset.as_ul }} instead of iterating through each field.
As for why the other listed solution (or the solution in the documentation) didn't work, I will remain baffled.
Related
Currently am using this textfield for my model
#model
class NoLoginPsuedoAppointment(models.Model):
...
comments = models.TextField(blank=True)
#form
class NoLoginPsuedoAppointmentForm(forms.ModelForm):
comments = forms.Textarea()
class Meta:
model = NoLoginPsuedoAppointment
fields = [
"comments",
]
Which comes out looking like this
Is there a way to change where the textarea begins so that comments is on top of the textarea or on its top left instead of being on its bottom left? Can this be changed through the django forms? or do I need to change it through css?
sure you can change it. that's only the form field label you can put it anywhere you want. I guess you rendered the form as {{ form.as_p }} or as table or ... so you have to loop through the form fields
<form ... >
{% csrf_token %} <!-- if thats POST -->
{% for field in form %}
<div class="row">
<div class="col-4">{{ field.label }}</div>
<div class="col-8">
{{ field }}
</div>
</div>
{% endfor %}
<button type="submit" class="btn btn-info">Save Changes</button>
</form>
in above example I just created a row and putted the label and field in the same row, you can put it as you wish. and also you can access the field errors with {{ field.errors }}.
I have a template which goes through a queryset and creates a checkbox for each item which seems to be a validation problem. I can only submit this when I check every checkbox and I just can't seem to figure out what is wrong.
my template:
<form method="POST">
{% csrf_token %}
<fieldset>
{% for choice in choices %}
{{ choice.description }}
<input type="checkbox" value="{{choice.section}}" name="sections" required="">
{% endfor %}
<button type="submit">Submit</button>
</fieldset>
</form>
my forms.py
class AddSectionForm(forms.Form):
sections = forms.MultipleChoiceField(
required=False, widget=forms.CheckboxSelectMultiple())
edit
Urghh, I'm an idiot, it's the required="" in the html checkbox object!
Thanks Willem, you got it! Also needed to change the checkbox values for the primary keys.
forms.py:
class AddSectionForm(forms.Form):
sections = forms.MultipleChoiceField(
required=True, widget=forms.CheckboxSelectMultiple(),
choices=Section.objects.all().values_list())
template:
<form method="POST">
{% csrf_token %}
<fieldset>
{% for choice in choices %}
{{ choice.description }}
<input type="checkbox" value="{{choice.pk}}" name="sections">
{% endfor %}
<button type="submit">Submit</button>
</fieldset>
Your checkboxes should not have required="", otherwise it is required to check these. Furthermore it is probably more elegant to use a ModelMultipleChoiceField:
class AddSectionForm(forms.Form):
sections = forms.ModelMultipleChoiceField(
queryset=Section.objects.all(),
required=True,
widget=forms.CheckboxSelectMultiple()
)
I have the following:
forms.py
class StoreSettingsForm(ModelForm):
enable_repricer = forms.BooleanField(required=True)
enable_ignore_min_price = forms.BooleanField(required=True)
class Meta:
model = Store
fields = '__all__'
exclude = ['user']
models.py
class Store(models.Model):
user = models.ForeignKey(User, models.SET_NULL, blank=True, null=True)
store_name = models.CharField(max_length=100, blank=False)
...
enable_repricer = models.BooleanField(default=False)
enable_ignore_min_price = models.BooleanField(default=False)
template.html
<form method="post">
{% csrf_token %}
<h4>Repricer Settings</h4>
<div class="row">
{{ form.non_field_errors }}
<div class="fieldWrapper col s4">
{{ form.enable_repricer.errors }}
<label for="{{ form.enable_repricer.id_for_label }}">Enable Repricer:</label>
{{ form.enable_repricer }}
</div>
<div class="fieldWrapper col s4">
{{ form.enable_ignore_min_price.errors }}
<label for="{{ form.enable_ignore_min_price.id_for_label }}">Allow Repricer to ignore min_price:</label>
{{ form.enable_ignore_min_price }}
</div>
<div class="fieldWrapper col s4">
{{ form.repricer_factor.errors }}
<label for="{{ form.repricer_factor.id_for_label }}">Repricer aggressiveness factor (1-100):</label>
{{ form.repricer_factor }}
</div>
</div>
<input class="btn" type="submit" value="Submit">
</form>
</div>
view.py
class StoreSettingsView(View):
template_name = 'store_settings.html'
def get(self, *args, **kwargs):
store = Store.objects.get(id=kwargs['id'])
data = {
'store_name': store.store_name,
'store_email': store.store_email,
...
'enable_ignore_min_price': store.enable_ignore_min_price,
'enable_repricer': store.enable_repricer,
'repricer_factor': store.repricer_factor,
}
form = StoreSettingsForm(initial=data)
return render(self.request, self.template_name, {
"store": store,
"form": form,
})
It does not show up in the form. All field are showing on the page but not the 2 boolean fields. The labels are showing and in the HTML.
I have excluded many fields from the code blocks to make it more easy to read.
I was with this problem and I solved it doing that:
#----- My error was caused by Materialize css ----#
Not showing checkbox
<form>
{% for field in form %}
{{field.label_tag}}<br>
{{field}}
<br>
<br>
{% endfor %}
</form>
Using Materialize, You must have a span tag in your label:
Example in Materialize doc:
<label>
<input type="checkbox" class="filled-in" checked="checked" />
<span>Filled in</span>
</label>
I solved the problem refacting to the Materialize's doc structure:
<form>
{% for field in form %}
<label>
{% if field.label == "colorida" %} <!-- 'colorida' is my boolean field -->
{{field}}
<span>{{field.label}}</span> <!-- if boolean field, span must be after input (in materialize) -->
{% else %}
<span>{{field.label}}</span> <!-- description before input if not boolean field -->
{{field}}
{% endif %}
</label>
<br>
<br>
{% endfor %}
</form>
Test your example on a page without any CSS, the problem probably is caused by CSS
Ok. When typing the question something arose to me. What if.... and yes that was it. In some way my CSS is disabling the checkbox and i really do not know why. When removing the CSS it works fine. Sorry.
Here are my forms:
class RoleForm(forms.ModelForm):
class Meta:
model = models.RoleModel
fields = ['name']
class FeatureForm(forms.ModelForm):
role = forms.ModelChoiceField(queryset=models.RoleModel.objects.values_list('name', flat=True).distinct())
class Meta:
model = models.FeatureModel
fields = ['role','feature']
In my bootstrap form, the choices display properly. I get a list of all roles. But if I fill the feature form and hit submit it says - "Select a valid choice. That choice is not one of the available choices."
My models are:
class RoleModel(models.Model):
name = models.CharField(validators=[alphanumeric], max_length=50, unique=True, blank=False)
class FeatureModel(models.Model):
role = models.ForeignKey(RoleModel, on_delete=models.PROTECT)
feature = models.CharField(validators=[alphanumeric], max_length=10, unique=True)
my bootsrtap form is:
<form action="{% url 'feature_form' %}" novalidate method="POST">{% csrf_token %}
<div class="row">
<div class="col">
<label for="role">{{ fform.role.label }}</label>
<p><select class="form-control id="role" name="role">
{% for item in fform.role %}
{{ item }}
{% endfor %}
</select></p>
{% for error in fform.role.errors %}
<p><small class="alert-danger">{{ error }}</small></p>
{% endfor %}
</div>
<div class="col">
<label for="feature">{{ fform.feature.label }</label>
<p><input type="text" class="form-control" id="feature" name="feature"
{% if fform.feature.value is not None %}
value="{{ fform.feature.value }}"
{% endif %}
></p>
{% for error in fform.feature.errors %}
<p><small class="alert-danger">{{ error }}</small></p>
{% endfor %}
</div>
</div>
<input class='btn btn-primary btn-sm' type='submit' value='Save'>
</form>
My need is simple. The second form (FeatureForm) has two fields. role being foreign key of another model and a text field to type in name of a feature. On my client side, I need the foreign key to be displayed as a select option with a list. I chose a value from this select, enter the value of feature and hit save. It has to save.
It doesn't work, because your queryset includes only names, but you need an id / primary key of a RoleModel. Since your choices don't have an id, they aren't a valid choice.
Firstly, your RoleModel name is set to unique and is therefore no point in querying distinct() name values because they will be distinct already by unique definition.
You also don't need to construct your own select input. Django will take care of this.
All you need is:
class RoleModel(models.Model):
name = models.CharField(validators=[alphanumeric], max_length=50, unique=True, blank=False)
def __str__(self):
return self.name
class FeatureForm(forms.ModelForm):
class Meta:
model = models.FeatureModel
fields = ['role', 'feature']
def __init__(self, *args, **kwargs):
super(FeatureForm, self).__init__(*args, **kwargs)
for field in self.fields:
self.fields[field].widget.attrs = {'class': 'form-control'}
and
<form action="{% url 'feature_form' %}" novalidate method="POST">
{% csrf_token %}
<div class="row">
<div class="col">
<label for="role">{{ fform.role.label }}</label>
<p>{{ fform.role }} </p>
{% for error in fform.role.errors %}
<p><small class="alert-danger">{{ error }}</small></p>
{% endfor %}
</div>
<div class="col">
<label for="feature">{{ fform.feature.label }</label>
<p>{{ fform.feature }}</p>
{% for error in fform.feature.errors %}
<p><small class="alert-danger">{{ error }}</small></p>
{% endfor %}
</div>
</div>
<input class='btn btn-primary btn-sm' type='submit' value='Save'>
</form>
EDIT: Otherwise, a Select input in this case should be constructed as:
<select>
<option value="1">Name 1</option>
<option value="2">Name 2</option>
</select>
where value is an id, a primary key of RoleModel in your case. Your select options don't have this value.
I create a form based on a model , like seen below:
class ContactSelectionForm(forms.ModelForm):
contacts = ManyToManyByLetter(Contact, field_name="first_name")
class Meta:
model = ContactSelection
exclude = ('created_by',)
When I process this view I see at the .html output a field labeled with "Contact".
Now I`m wondering whether it is possible to change this output. For example I want to name this field not "Contact" but "Selected Contacts".
This is the form processing part of the .html template:
<form action="{{ request.path }}" method="POST">
<div>
<fieldset class="module aligned">
{% for field in form.visible_fields %}
<div class="form-row">
{# Include the hidden fields in the form #}
{% if forloop.first %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% endif %}
{{ field.errors }}
{{ field.label_tag }} {{ field }}
</div>
{% endfor %}
<p><input type="submit" value="Save" /></p>
</fieldset>
</div>
</form>
If somebody is wondering what ManyToManyByLetter(Contact, field_name="first_name") in the form is, check out http://code.google.com/p/django-ajax-filtered-fields/ . A very helpful many2many javascript library.
Did you try setting the fields label?
(the docs)
contacts = ManyToManyByLetter(Contact, field_name="first_name", label="Selected Contacts")