Extra form on ModelFormSet - python

For my Django project, I am rendering the model formset election_formset = modelformset_factory(Election, exclude=('Complete',), formset=BaseElectionFormSet) in my template:
<form method="post" action="">
{{ formset.management_form }}
{% for form in formset %}
<div class='card'>
<div class='card-body w-75 mx-auto'>
<div class='row'>
<div class='col-6 text-center'>
<p>Name<br>{{form.Name}}</p>
</div>
<div class='col-6 text-center'>
<p>Videos<br>{{form.FlipGrid}}</p>
</div>
</div>
<div class='row'>
<div class='col-12 text-center'>
<p>Description<br>{{form.Description}}</p>
</div>
</div>
<div class='row'>
<div class='col-6 text-center'>
<p>Allow registration: {{form.CandidateReg}}</p>
</div>
<div class='col-6 text-center'>
<p>Allow voting: {{form.VotingOpen}}</p>
</div>
</div>
</div>
</div>
{% endfor %}
</form>
When the formset renders, an extra, blank form is shown at the end of the forms. I only want forms to show that are instances of existing records. Why is there an extra blank formset and how can I prevent it?

Try this
election_formset = modelformset_factory(
Election,
exclude=('Complete',),
formset=BaseElectionFormSet,
extra=0
)
The extra keyword defaults to 1 otherwise (see docs):
def modelformset_factory(model, form=ModelForm, formfield_callback=None,
formset=BaseModelFormSet, extra=1, can_delete=False,
can_order=False, max_num=None, fields=None, exclude=None,
widgets=None, validate_max=False, localized_fields=None,
labels=None, help_texts=None, error_messages=None,
min_num=None, validate_min=False, field_classes=None):

Try to use max_num parameter to limit exta if there are some initial data in formset:
election_formset = modelformset_factory(Election, exclude=('Complete',), formset=BaseElectionFormSet, max_num=1)
From the docs:
If the number of items in the initial data exceeds max_num, all initial data forms will be displayed regardless of the value of max_num and no extra forms will be displayed.

Related

Can not save form with CreateView (Django, Python)

I am trying to save data that fill out with form to database but it doesn't work as well. when i click on Submit button nothing change, the webpage just refresh!
These are my codes.
Views.py
class HotelAdCreate(AuthorsAccessMixin,CreateView):
model = HotelBookingAd
form_class = HotelBookingAdForm
template_name = "account/article-create-update.html"
def form_valid(self,form):
form.save()
return super(HotelAdCreate).form_valid(form)
Forms.py
class HotelBookingAdForm(forms.ModelForm):
class Meta:
model = HotelBookingAd
fields = '__all__'
def clean_sales_price(self):
sales_price = self.cleaned_data["sales_price"]
purchase_price = self.cleaned_data["purchase_price"]
if sales_price > purchase_price:
raise forms.ValidationError("error.")
print("error")
return sales_price
Edit :
i just added template file codes to debaug clearly.
Template File
{% extends 'account/base.html' %}
{% load crispy_forms_tags %}
{% block title %}{% endblock %}
{% block title-meta %} آگهی{% endblock %}
{% block main %}
<div class="col-md-12">
<div class="card card-primary">
<div class="card-header">
<h3 class="card-title- mb-0">ایجاد آگهی </h3>
</div>
<div class="card-body">
<form method="post" enctype="multipart/form-data">{% csrf_token %}
<div class="row">
<div class="col-6">
{{form.title|as_crispy_field}}
</div>
<div class="col-6">
{{form.hotel_name|as_crispy_field}}
</div>
<div class="col-12">
{{form.sale_reason|as_crispy_field}}
</div>
<div class="col-6">
{{form.check_in_date|as_crispy_field}}
</div>
<div class="col-6">
{{form.check_out_date|as_crispy_field}}
</div>
<div class="col-6">
{{form.purchase_price|as_crispy_field}}
</div>
<div class="col-6">
{{form.sales_price|as_crispy_field}}
</div>
<div class="col-6">
{{form.city_name|as_crispy_field}}
</div>
<div class="col-6">
{{form.room_type|as_crispy_field}}
</div>
<div class="col-6">
{{form.confirm|as_crispy_field}}
</div>
{% if user.is_superuser %}
<div class="col-6">
{{form.slug_generator|as_crispy_field}}
</div>
{% endif %}
</div>
<button class="btn btn-success">ارسال مقاله</button>
{% if user.is_superuser and request.resolver_match.kwargs.pk %}
<a class="btn btn-danger "href="{% url 'account:hotelad-delete' request.resolver_match.kwargs.pk %}">
حذف مقاله
</a>
<a target="_blank" class="btn btn-dark "href="{% url 'primary:preview' request.resolver_match.kwargs.pk %}">
پیش نمایش
</a>
{% endif %}
</form>
</div>
</div>
</div>
{% endblock %}
I've tried different options, but I don't have enough skills to handle this problem.
Can anyone help me?
UPDATE (FIXED)
I found problem by my self, here it is :
As you see in forms.py, i am using __all__ for fields but in template i am not using all the fields, so form will not be valid.
To fix the bug completely i just added Exclude option and added the fields that i didn't use in template. finally everything works fine :)

How to access form data in Flask_WTF Form before the form is submitted?

I am creating a form to find other group members, where all members belongs to departments. Some department, but not all, departments have an internal id that can be used to identify each member. Each department also has a different term for this internal member id. Department A calls it "personal identifier," department B calls it "unique id," etc. In the database departments table, the member_id_term field stores what the department calls the internal member id. In the members table in the database, the internal_id stores the member's internal_id if they belong to a department with the internal_id system.
On the first page of the form, the user selects the department, and on the second page of the form, if the user has selected a department that uses internal member ids, they are asked to put the internal member id.
My strategy was to create a variable in views.py called member_id_term and set it to 0 if there is no form.dept.data, but to set member_id_term to form.dept.data.member_id_term when form.dept.data exists. Then, in input_find_member.html, I would conditionally display the input field for internal_id if member_id_term exists.
Problem: I don't have access to form.dept.data until I submit the form. How can I access this value before submitting the form?
Here is the code I currently have. It throws an error on member_id_term = 0 if not form.dept.data else form.dept.data.member_id_term and says that form.dept.data is none.
forms.py
class FindMemberForm(Form):
name = StringField(
'name', default='', validators=[Regexp('\w*'), Length(max=50),
Optional()]
)
internal_id = StringField('internal_id', default='', validators=[Regexp('\w*'), Length(max=55)])
dept = QuerySelectField('dept', validators=[DataRequired()],
query_factory=dept_choices, get_label='name')
views.py
#main.route('/find', methods=['GET', 'POST'])
def get_officer():
form = FindMemberForm()
member_id_term = 0 if not form.dept.data else form.dept.data.member_id_term
if form.validate_on_submit():
return redirect(url_for('main.get_gallery'), code=307)
return render_template('input_find_member.html', form=form)
{% extends "base.html" %}
{% block content %}
and here is input_find_member.html
<div role="main">
<div class="hero-section no-sub">
<div class="hero">Find a Member </div>
</div>
</div>
<div class="container" role="main">
<form action="{{ url_for('main.get_member') }}" method="post" class="form">
{{ form.hidden_tag() }}
<div class="row form-group">
<div class="col-xs-12">
<ul class="nav nav-pills nav-justified thumbnail setup-panel">
<li class="active"><a href="#step-1">
<h4 class="list-group-item-heading">Step 1</h4>
<p class="list-group-item-text">Department Information</p>
</a></li>
<li class="disabled"><a href="#step-2">
<h4 class="list-group-item-heading">Step 2</h4>
<p class="list-group-item-text">Member Info </p>
</a></li>
</ul>
</div>
</div>
<div class="row setup-content" id="step-1">
<div class="col-xs-12">
<div class="col-md-12 well text-center">
<h2><small>Select Department</small></h2>
<div class="input-group input-group-lg col-md-4 col-md-offset-4">
{{ form.dept(class="form-control") }}
</div>
{% for error in form.dept.errors %}
<p>
<span style="color: red;">[{{ error }}]</span>
</p>
{% endfor %}
<br>
<button id="activate-step-2" class="btn btn-primary btn-lg">Next Step</button>
</div>
</div>
</div>
<div class="row setup-content" id="step-2">
<div class="col-xs-12">
<div class="col-md-12 well text-center">
<h2><small>Do you know the member's name?</small></h2>
<div class="input-group input-group-lg col-md-4 col-md-offset-4">
{{ form.name(class="form-control") }}
{% for error in form.name.errors %}
<p><span style="color: red;">[{{ error }}]</span></p>
{% endfor %}
</div>
{% if member_id_term %}
<h2><small>Do you know the member's {{ member_id_term }}?*</small></h2>
<div class="input-group input-group-lg col-md-4 col-md-offset-4">
{{ form.internal_id(class="form-control") }}
{% for error in form.internal_id.errors %}
<p><span style="color: red;">[{{ error }}]</span></p>
{% endfor %}
</div>
{% endif %}
<br>
<button id="activate-step-3" class="btn btn-primary btn-lg">Next Step</button>
</div>
</div>
</div>
</form>
</div>
I've already read these pages
Flask WTForms: how do I get a form value back into Python?
Get data from WTForms form
populate WTForms select field using value selected from previous field
Thanks for reading!

Django 2 form in modal

Im tries to open in Django the user edit form in Bootstrap modal. But the form is empty, only the save button is shown. But I don't understand how I can make the connection. If I call the edit page directly, then I can edit the user
127.0.0.1:8000/account/edit/
index.html, includes the referral to the form
{% extends 'base.html' %}
{% block head %}
{% endblock %}
{% block body %}
<div class="container-fluid">
<div class="row">
<div class="col-sm-12 col-md-6">
<div class="panel panel-default">
<div class="panel-body">
{% if error_message %}
<p><strong>{{ error_message }}</strong></p>
{% endif %}
<form action="{% url 'account:edit_profile' %}">
<input type="submit" value="Edit" />
</form>
<form action="{% url 'account:change_password' %}">
<input type="submit" value="Change Login" />
</form>
<br>
Open Modal
<br>
<div class="control-label col-sm-2">
First name:
</div>
<div class="col-sm-2">
{{ user.first_name }}
</div><br>
<div class="control-label col-sm-2">
Last name:
</div>
<div class="col-sm-2">
{{ user.last_name }}
</div><br>
<div class="control-label col-sm-2">
Email:
</div>
<div class="col-sm-2">
{{ user.email }}
</div>
</div>
</div>
</div>
</div>
</div>
<div class="modal fade" id="edit-profile-modal" >
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header" align="center">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
</button>
</div>
<div id="div-forms">
{% include "account/edit_profile.html" with form=form %}
</div>
</div>
</div>
</div>
{% endblock %}
edit_profile.html
{% block head %}
{% endblock %}
{% block body %}
<div class="container-fluid">
<div class="row">
<div class="col-sm-12 col-md-6">
<div class="panel panel-default">
<div class="panel-body">
<h3>Profile</h3>
{% if error_message %}
<p><strong>{{ error_message }}</strong></p>
{% endif %}
<form method="post">
{% csrf_token %}
{{ user_form.as_p }}
<button type="submit">Save</button>
</form>
</div>
</div>
</div>
</div>
</div>
{% endblock %}
views.py
def edit_profile(request):
if request.method == 'POST':
user_form = EditUserForm(request.POST, instance=request.user)
if all([user_form.is_valid(), profile_form.is_valid()]):
user_form.save()
return render(request, 'account/index.html')
else:
user_form = EditUserForm(instance=request.user)
args = {'user_form': user_form}
return render(request, 'account/edit_profile.html', args)
urls.py
urlpatterns = [
...
url(r'^edit/$', views.edit_profile, name='edit_profile'),
...
]
forms.py
class EditUserForm(forms.ModelForm):
class Meta:
model = User
fields = (
'email',
'first_name',
'last_name'
)
Im using:
Python 3.6.3
Django 2.0.7
Windows 8.1
Bootstrap 3.3.6
JQuery 1.12.0
I think that variable form doesn't exist and you use in template just user_form not form variable
{% include "account/edit_profile.html" with form=form %}
Try use it:
{% include "account/edit_profile.html" with user_form=user_form %}
Maybe you could try the code I wrote and you can find it at django-bootstrap-modal-forms. You will be able to bind your form to the modal and all of the validation stuff will work out of the box.
You will create a trigger element opening the modal
Your selected form will be appended to the opened modal
On submit the form will be POSTed via AJAX request to form's URL
Unsuccessful POST request will return errors, which will be shown under form fields in modal
Successful POST request will redirects to selected success URL

Django ModelFormSet not submitting correctly

I am having trouble submitting a modelformset in my view. For some reason, the formset is not meeting is_valid criteria, whatever I set the actual data to.
Here is the relevant part of the view:
def admin_tools(request):
ElectionFormSet = modelformset_factory(Election, exclude=('Complete',), formset=BaseElectionFormSet, extra=0)
if request.method == 'POST':
if 'edit_elections' in request.POST:
election_formset = ElectionFormSet(request.POST)
# print(formset)
if election_formset.is_valid():
election_formset.save()
messages.add_message(request, messages.SUCCESS, 'Election settings saved')
return redirect(reverse('elections:home'))
else:
messages.add_message(request, messages.ERROR, 'Problem')
return redirect(reverse('elections:home'))
else:
election_formset = ElectionFormSet()
return render(request, 'admin/admin_tools.html',{
'formset': election_formset,
})
Every time I submit the formset I get the problem message indicating the formset is not valid and I don't know why.
Here is form tag in the template:
<form method="post" action="">
{{ formset.management_form }}
{% for form in formset %}
{% csrf_token %}
<div class='card'>
<div class='card-body w-75 mx-auto'>
<div class='row'>
<div class='col-6 text-center'>
<p>Name<br>{{form.Name}}</p>
</div>
<div class='col-6 text-center'>
<p>Videos<br>{{form.FlipGrid}}</p>
</div>
</div>
<div class='row'>
<div class='col-12 text-center'>
<p>Description<br>{{form.Description}}</p>
</div>
</div>
<div class='row'>
<div class='col-6 text-center'>
<p>Allow registration: {{form.CandidateReg}}</p>
</div>
<div class='col-6 text-center'>
<p>Allow voting: {{form.VotingOpen}}</p>
</div>
</div><p>{{form.id}}</p>
</div>
</div>
{% endfor %}
<div class='text-center'>
<br><button type="submit" class='btn btn-outline-dark' name='edit_elections'>Save</button>
</div>
</form>
I'm not sure what the error would be here. My thought is that maybe each form should be submitted individually and not as a whole formset? I'm also not sure my csrf token is in the right place.
How do I get my formset to submit correctly?
When the formset is invalid, you typically don't redirect. That allows you to re-display the formset along with errors.
In your case, you are rendering the formset manually, so you are not displaying any errors. You could start with the simplest possible template.
<form method="post" action="">
<table>
{{ formset }}
</table>
</form>
Once you're happy the view works, you can customize the template more. See the docs on using formsets in templates for more info.
If you really want to redirect when the formset is invalid, you can check check formset.errors in your view to debug the problem.

MultiValueDictKeyError when submitting ModelFormSet

When trying to submit my ModelFormSet I am getting a MultiValueDictKeyError. The error message is not very descriptive so I'm not sure why the error is being thrown.
Here is the view:
def admin_tools(request):
ElectionFormSet = modelformset_factory(Election, exclude=('Complete',), formset=BaseElectionFormSet, extra=0)
if request.method == 'POST':
if 'new_election' in request.POST:
new_election = NewElectionForm(request.POST)
if new_election.is_valid():
election = new_election.save(commit=False)
election.save()
messages.add_message(request, messages.SUCCESS, 'Election created')
return redirect(reverse('elections:home'))
elif 'edit_elections' in request.POST:
formset = ElectionFormSet(request.POST)
if formset.is_valid():
formset.save()
messages.add_message(request, messages.SUCCESS, 'Election settings saved')
return redirect(reverse('elections:home'))
else:
new_election_form = NewElectionForm()
formset = ElectionFormSet()
return render(request, 'admin/admin_tools.html',{
'new_election': new_election_form,
'formset': formset,
})
Here is the relevant section of the template:
<div class="card-body">
<h4>Toggle election settings:</h4>
<form method="post" action="">
{{ formset.management_form }}
{% for form in formset %}
{% csrf_token %}
<div class='card'>
<div class='card-body w-75 mx-auto'>
<div class='row'>
<div class='col-6 text-center'>
<p>Name<br>{{form.Name}}</p>
</div>
<div class='col-6 text-center'>
<p>Videos<br>{{form.FlipGrid}}</p>
</div>
</div>
<div class='row'>
<div class='col-12 text-center'>
<p>Description<br>{{form.Description}}</p>
</div>
</div>
<div class='row'>
<div class='col-6 text-center'>
<p>Allow registration: {{form.CandidateReg}}</p>
</div>
<div class='col-6 text-center'>
<p>Allow voting: {{form.VotingOpen}}</p>
</div>
</div>
</div>
</div>
{% endfor %}
<div class='text-center'>
<br><button type="submit" class='btn btn-outline-dark' name='edit_elections'>Save</button>
</div>
</form>
</div>
The error is raise MultiValueDictKeyError(repr(key))
django.utils.datastructures.MultiValueDictKeyError: "'form-0-id'"
and it was flagged on the line: if formset.is_valid(): of the view.
How do I resolve this error and get the formset to correctly submit and update the data in the model?

Categories

Resources