Render Django Formset Manually - python

When i render my formset using a loop, everything works.
When i try to render it manually by accessing each field separately ( for frontend purpose ) the form is rendering but the submit fail. Every fields are the same, so i guess there is a hidden field created when working with the formset that i dont know about.
Here a simplified sample of my working code
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ formset.management_form }}
{% for p in formset %}
{{p.as_p}}
{% endfor %}
</form>
And a simplified example of what is not working
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ formset.management_form }}
{% for p in formset %}
<span class="form-sub-label-container " style="vertical-align:top">
{{p.field1}}
<label class="form-sub-label" for="input_12_city" id="sublabel_12_city" style="min-height:13px"></label>
</span>
< span class="another_span">
{{p.field2}}
</span>
## etc....
{% endfor %}
</form>
Any idea ?
Thanks.

If you want to render each formset form field manually, you have to add Django default hidden fields.
<form method="post">
{% csrf_token %}
{{ formset.management_form }}
{% for p in formset %}
{{ p.id }} # if formset is ModelFormSet
{{ p.ORDER }} # if can_order=True
{{ p.DELETE }} # if can_delete=True
... # your custom fields
{% endfor %}
</form>

Related

Python, Django: Editing inlineformset_factory

inside my app I'm using an inlineformset_factory and right know it's working fine! But when displaying it inside my template the labels are always staying right above the input-field. Is there any way to display them side-by-side or even move the label as some sort of placeholder inside the input-field?
views.py
formset = inlineformset_factory(Model_A, Model_B, can_delete=False, extra=0, fields=('fields_01', 'fields_02', 'fields_03'))
template.html
<form method="post">
{% csrf_token %}
{{ formset.management_form }}
{% for form in formset %}
{{ form }}
{% endfor %}
<button type="submit">Save</button>
</form>
Thanks for all your help and have a great weekend!
Try this:
<form action="" method="POST">
{% csrf_token %}
{{ form.management_form }}
{% for field in form %}
{{field.product.label}} - {{field.product}} #here product is my field name
<hr>
{% endfor %}
<input type="submit" name="Submit">
</form>
You have to do this for every field
#Changes
<form method="post">
{% csrf_token %}
{{ formset.management_form }}
{% for form in formset %}
{{ form.Field_name.label }} - {{form.Field_name}} #1st field
{{ form.Field_name.label }} - {{form.Field_name}} #2nd field
{% endfor %}
<button type="submit">Save</button>
</form>
I think the displaying was influenced by css。
Use F12 to check css in chrome to find the reason。
You can edit the css to change the displaying.
Or use django-widgets-improved and bootstrap to set class to the fields of form.
{% load widget_tweaks %}
<form method="post">
{% csrf_token %}
{{ formset.management_form }}
{% for form in formset %}
{% for field in form %}
{% render_field field class="form-control" %}
{% endfor %}
{% endfor %}
<button type="submit">Save</button>
</form>

Adding content between form in Formset_Factory = Django

I am working on a django application and more specifically, a formset (group of forms). I have the form set amount set the number of objects retrieved by a query set. What I want to do is make a change to the form set so that in between each of the forms in the formset, include a name for a user related to the form set. What is happening right now is that it is print the entire formset then the names that I want to move to be placed in between each of the forms in the formset.
** updated **.
Now what is happening is that between each of the different forms that are being iterated, It is displaying all of the objects that are being displayed. My issue is that I only want the first object in the list to print before the first form, second objects to display before the second form. and so on...
Sample of what is happening:
Add expense - restructured group
Please complete the form below
josh
lifter
omar
Amount: 0
Description: expense
josh
lifter
omar
Amount: 0
Description: expense
josh
lifter
omar
Amount: 0
Description: expense
submit
I want it to just display the first name for first form and so on. Here is the code:
{% extends "base.html" %}
{% block content %}
<h2>Add expense - {{ currentGroup.name }}</h2>
{% if message %}
<p>{{message}}</p>
{% endif %}
<form action="." method="POST">
{% csrf_token %}
{{ form.management_form }}
{% for f in form %}
{% for expense in expenses %}
<p>{{ expense.user.username }}</p>
{% endfor %}
{{ f.as_p }}
{% endfor %}
<input type="submit" name="submit" value="submit">
</form>
{% endblock %}
You can iterate form var in template and can add information between form but make sure you have to add {{ form.management_form }} also in form like below code
<form action="." method="POST">
{% csrf_token %}
{{ form.management_form }}
{% for f in form %}
{# Add whatever information you want to show between forms #}
{{ f.as_p }}
{% endfor %}
{% for expense in expenses %}
<p>{{ expense.user.username }}</p>
{% endfor %}
<input type="submit" name="submit" value="submit">
</form>

Django formset - not setting initial data when data is passed

I am creating form using Django Form wizard and formsets.
def get_form(self, step=None, data=None, files=None):
initial_data_set = []
for x in some_list:
initial_data_set.append({
'title' : x.title,
})
data = {
'form-TOTAL_FORMS': '5',
'form-INITIAL_FORMS': '5',
'form-MAX_NUM_FORMS': '',
}
formset_class = formset_factory(TitleForm, extra =0)
formset = formset_class(data=data, initial=initial_data_set)
return formset
Template
{% extends "admin/base_site.html" %}
{% load i18n %}
{% block content %}
{% if wizard.form.forms %}
{% for form in wizard.form.forms %}
{{ form.media }}
{% endfor %}
{% else %}
{{ wizard.form.media }}
{% endif %}
<p>Step {{ wizard.steps.step1 }} of {{ wizard.steps.count }}</p>
<form action="." method="post" enctype="multipart/form-data">{% csrf_token %}
<table>
{{ wizard.management_form }}
{% if wizard.form.forms %}
{{ wizard.form.management_form }}
{% for form in wizard.form.forms %}
{% if ingestable_upload %}
<tr>{{ form.as_inline_table }}</tr>
{% else %}
{{ form.as_table }}
{% endif %}
{% endfor %}
{% else %}
{{ wizard.form }}
{% endif %}
</table>
{% if wizard.steps.prev %}
<button name="wizard_goto_step" type="submit" value="{{ wizard.steps.first }}">{% trans "first step" %}</button>
<button name="wizard_goto_step" type="submit" value="{{ wizard.steps.prev }}">{% trans "prev step" %}</button>
{% endif %}
<input type="submit" value="{% trans "Submit" %}"/>
</form>
{% endblock %}
I was able to see initial data in my form when I was not passing 'data'. However, not passing data was giving me formset.is_valid as False and there was None in the cleaned_data. So I created data{} and passed it as per the documentation here -
https://docs.djangoproject.com/en/1.7/topics/forms/formsets/#understanding-the-managementform
Since I started passing data, form is not getting populated with initial data.
I have put debug statements in formsets.py under BaseFormSet() class. It is getting both data and initial data that I am passing.
I have been struggling with this for few days. Any help on how I can populate my form and get cleaned data will be great.
What you're doing is overriding your form initial data with your data object, data attribute is not necessary that data is passed on the {{ formset.management_form }} in your template.
There's something else wrong with your form. I would suggest you print the errors when is_valid fails and see what's missing.

How to get a certain field out of form in django template?

i wonder how i can add some extra html to a certain field in a template in Django
I have the following form (forms.py)
first_name = forms.CharField(widget=forms.TextInput(attrs={'required': 'required'}))
last_name = forms.CharField(widget=forms.TextInput(attrs={'required': 'required'}))
street = forms.CharField(widget=forms.TextInput(attrs={'required': 'required'}))
In my template i get the fields with:
<form action="" method="post">{% csrf_token %}
{% for field in form %}
<div class="fieldWrapper">
{{ field.errors }}
{{ field.label_tag }} {{ field }}
</div>
{% endfor %}
<input type="submit" value="Submit" />
</form>
Now i want to add some extra html to the street field like this
<form action="" method="post">{% csrf_token %}
{% for field in form %}
{% if field.type =='street' %}
<div class="fieldWrapper otherclass">
<hr>
{% else %}
<div class="fieldWrapper">
{% endif %}
{{ field.errors }}
{{ field.label_tag }} {{ field }}
</div>
{% endfor %}
<input type="submit" value="Submit" />
</form>
The whole thing fails:
Could not parse the remainder: '=='street'' from '=='street''
Template operators require a space before and after so replace
{% if field.type =='street' %}
with
{% if field.type == 'street' %}
or use the ifequal templatetag
{% ifequal field.type 'street' %}
Hello world.
{% else %}
Apocalypse
{% endifequal %}

Django forms adding <div> after form field

Below is my form code :
class FMessage(forms.Form):
From = forms.CharField()
To = forms.CharField()
Subject = forms.CharField()
Message = forms.CharField()
and this is my html code:
<form method='POST' action='.'>
{% csrf_token %}
{{ form.as_p }}
<input type='submit' value='submit'>
</form>
The code works fine by displaying forms and has not any issue in functionality, but now I need to wrap my form fields in html by a div like this:
<div id='mydiv'>
<input ... />
<div>
How can I fix it?
Seems like you do not really want to use the inbuilt <p> or <table> wrapped forms and rather want to display the fields wrapped within a <div>'s. You can simply iterate over fields in the form as follows.
{% if form %}
<!-- Form Errors -->
{% if form.errors %}
<ul class="errors">
{% for error in form.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
<!-- Display Form -->
<form>
{% csrf_token %}
{% for field in form %}
<div class="mydiv">
<label class="mylabel">{{ field.label }}</label>
{{ field }}
</div>
{% endfor %}
</form>
{% endif %}
Dont render the form by using form.as_p. You need to show each field of the form, for example, by using form.to. By using this way, you can wrap the field 'to' into a div
<div>{{ form.To}} </div>
For more detail, view this link

Categories