Django FormWizard dynamic forms - django

I am trying to create a FormWizard to add multiple instances of a model at once. The second page is dynamic, based on the number to be added, and creates a single "serial number" field - the differentiating factor between all the instances.
I'm getting the first page of the wizard fine, but the second page - the dynamic one - is giving me this Validation Error:
[u'ManagementForm data is missing or has been tampered.']
All the searching I've done points to a formset issue, which I'm not using. What have I done wrong?
forms.py
class CarrierWizardForm1(forms.Form):
part_numbers = forms.ModelChoiceField(queryset=PartNumber.objects.all())
expiry_date = forms.DateField()
qty_at_new = forms.IntegerField()
unit_cost = forms.DecimalField(max_digits=10, min_value=0, decimal_places=3)
cost_currency = forms.ChoiceField(choices=CURRENCY_CHOICES)
class CarrierWizardForm2(forms.Form):
def __init__(self, *args, **kwargs):
qty = kwargs.pop('qty')
super(CarrierWizardForm2,self).__init__(*args,**kwargs)
for i in qty:
self.fields['serial_%s' % i] = forms.CharField(max_length=45)
The defs in CarrierWizardForm2 is a fairly common idiom that is noted all over the web as a solution to this problem, including by Jacobian Nor is their anything crazy in urls
urls.py
carrier_wizard_forms = [CarrierWizardForm1, CarrierWizardForm2]
...
url(r'^carrier/add/$', views.CarrierCreateWizard.as_view(carrier_wizard_forms)),
My views are relatively complex, but nothing outrageous. Note there are some fields that aren't in the forms - they are filled with context data (user, creation date, etc)
views.py
TEMPLATES = {"0": "inventory/carrier_wizard_form1.html",
"1": "inventory/carrier_wizard_form2.html"}
class CarrierCreateWizard(SessionWizardView):
def get_template_names(self):
return [TEMPLATES[self.steps.current]]
''' Get the qty from form 1 to indicate how many fields
form2 needs for serial numbers
'''
def get_form_initial(self, step):
current_step = self.storage.current_step
if current_step == 'step1':
prev_data = self.storage.get_step_data('step0')
return self.initial_dict.get(step, {'qty': qty})
return self.initial_dict.get(step, {})
def done(self, form_list, **kwargs):
details = form_list[0]
list_of_serial_nos = form_list[1]
for serial_name, serial in list_of_serial_nos.cleaned_data.items():
carrier = Carrier()
carrier.part_numbers = details.cleaned_data['part_numbers']
carrier.creation_date = datetime.datetime.today()
carrier.expiry_date = details.cleaned_data['expiry_date']
carrier.qty_at_new = 1
carrier.qty_current = 1
carrier.serial = serial
carrier.unit_cost = details.cleaned_data['unit_cost']
carrier.cost_currency = details.cleaned_data['cost_currency']
carrier.user = self.request.user
carrier.save()
My second template is bland, although you can see three unsuccessful attempts to rectify this issue.
inventory/carrier_wizard_form2.html
{% extends "base.html" %}
{% block content %}
<p>Step {{ wizard.steps.step1 }} of {{ wizard.steps.count }}</p>
<form action="" method="post">{% csrf_token %}
{# Attempt 3 #}
{{ formset }}
{# Attempt 2 {{ form.management_form }}
{% for f in form %}
{{ f.as_p }}
{% endfor %}
#}
{# Attempt 1 {{ form }} #}
<input type=submit>
</form>
{% endblock %}
EDIT
*As requested, both of my templates*
inventory/carrier_wizard_form1.html
{% extends "base.html" %}
{% block content %}
<p>Step {{ wizard.steps.step1 }} of {{ wizard.steps.count }}</p>
<form action="" method="post">{% csrf_token %}
{{ wizard.form.as_p }}
<input type=submit>
</form>
{% endblock %}
templates/inventory/carrier_wizard_form2.html
{% extends "base.html" %}
{% block content %}
<p>Step {{ wizard.steps.step1 }} of {{ wizard.steps.count }}</p>
<form action="" method="post">{% csrf_token %}
<table>
{{ wizard.form }}
{#
{{ wizard.management_form }}
{% if wizard.form.forms %}..
{{ wizard.form.management_form }}
{% for form in wizard.form.forms %}
{{ form }}
{% endfor %}
{% else %}
{{ wizard.form }}.
{% endif %} #}
</table>
<input type=submit>
</form>
{% endblock %}

Your template is not correct, use {{ wizard.management_form }} to add wizard management related stuff and use {{ wizard.form }} for forms.
From the reference doc the template is like:
{% extends "base.html" %}
{% block content %}
<p>Step {{ wizard.steps.step1 }} of {{ wizard.steps.count }}</p>
<form action="" method="post">{% csrf_token %}
{{ wizard.management_form }} {#add wizard management data #}
{% if wizard.form.forms %} {# if form is formset #}
{{ wizard.form.management_form }} {#formset's management data #}
{% for form in wizard.form.forms %}
{{ form }}
{% endfor %}
{% else %}
{{ wizard.form }} {#for normal 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>

Related

Why this django formset has stopped saving the content all of a sudden?

I had this view that rendered a form and a formset in the same template:
class LearnerUpdateView(LearnerProfileMixin, UpdateView):
model = User
form_class = UserForm
formset_class = LearnerFormSet
template_name = "formset_edit_learner.html"
success_url = reverse_lazy('pages:home')
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
learner = User.objects.get(learner=self.request.user.learner)
formset = LearnerFormSet(instance=learner)
context["learner_formset"] = formset
return context
def get_object(self, queryset=None):
user = self.request.user
return user
def post(self, request, *args, **kwargs):
self.object = self.get_object()
form_class = self.get_form_class()
form = self.get_form(form_class)
user = User.objects.get(learner=self.get_object().learner)
formsets = LearnerFormSet(self.request.POST, request.FILES, instance=user)
if form.is_valid():
for fs in formsets:
if fs.is_valid():
# Messages test start
messages.success(request, "Profile updated successfully!")
# Messages test end
fs.save()
else:
messages.error(request, "It didn't save!")
return self.form_valid(form)
return self.form_invalid(form)
Then i wanted to make it prettier and i added the select2 multicheckbox widget and the django-bootstrap-datepicker-plus
Nothing has changed elsewhere, yet when i submit the post it only saves the data relative to User and not to Learner (which relies on the formset)
According to the messages, the formset data is not validated, I don't understand why since i didn't touch the substance at all but just the appearance.
Being a beginner im probably missing something big, I thank in advance whoever can help me find out the problem.
Here below the forms and the template:
(users.forms)
class LearnerForm(forms.ModelForm):
class Meta:
model = Learner
fields = ['locations', 'organization', 'supervisor', 'intro']
class UserForm(forms.ModelForm):
class Meta:
model = User
fields = ['username', 'first_name', 'last_name', 'birthday', 'email', 'profile_pic']
widgets = {
'birthday': DatePickerInput(format='%Y-%m-%d'), }
LearnerFormSet = inlineformset_factory(User, Learner, form=LearnerForm)
template
{% extends '_base.html' %}
{% load crispy_forms_tags %}
{% block content %}
<form action="" method="POST" enctype="multipart/form-data">
{% csrf_token %}
{{ form|crispy }}
{{ learner_formset.management_form}}
{% for form in learner_formset %}
{% if forloop.first %}
{% for field in form.visible_fields %}
{% if field.name != 'DELETE' %}
<label for="id_{{ field.name }}">{{ field.label|capfirst }}</label>
<div id='id_{{ field.name }}' class="form-group">
{{ field.errors.as_ul }}
{{ field }}
</div>
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
<input class="btn btn-success" type="submit" value="Update"/>
</form>
{% endblock content %}
I figured it out by combing the previous commits. The problem was in the template, the formset wasn't getting validated because i had ignored the hidden fields, which i hear are a common problem when dealing with formsets.
So the correct code is as such:
{% extends '_base.html' %}
{% load crispy_forms_tags %}
{% block content %}
<form action="" method="POST" enctype="multipart/form-data">
{% csrf_token %}
{{ form|crispy }}
{{ learner_formset.management_form}}
{% for form in learner_formset %}
{% if forloop.first %}
{% comment %} This makes it so that it doesnt show the annoying DELETE checkbox {% endcomment %}
{% for field in form.visible_fields %}
{% if field.name != 'DELETE' %}
<label for="{{ field.name }}">{{ field.label|capfirst }}</label>
<div id="{{ field.name }}" class="form-group">
{{ field }}
{{ field.errors.as_ul }}
</div>
{% endif %}
{% endfor %}
{% endif %}
{% for field in form.visible_fields %}
{% if field.name == 'DELETE' %}
{{ field.as_hidden }}
{% else %}
{# Include the hidden fields in the form #}
{% if forloop.first %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% endif %}
{% endif %}
{% endfor %}
{% endfor %}
<input class="btn btn-success" type="submit" value="Update"/>
</form>
{% endblock content %}

How to create and submit multiple instances of a form on a single page?

I want 3 instance of a URL input form so I can submit up to 3 different URLs.
forms.py
class AdditemForm(forms.Form):
url = forms.URLField(
label='Add Item',
widget=forms.URLInput(
attrs={
"class": "form-control",
}))
view.py
def ItemDetail(request, pk):
listitem = comparelist.objects.get(id=pk)
if request.method == 'POST':
form = AdditemForm(request.POST)
if form.is_valid():
url = form.cleaned_data.get("url")
items.objects.create(
link=url,
name=product_name,
price=price,
store=store,
)
return HttpResponseRedirect(request.path_info)
else:
form = AdditemForm()
template = 'itemdetail.html'
context = {
"comparelist": listitem,
"form": form,
}
return render(request, template, context)
I'm using a form snippet I found in a tutorial:
{% load widget_tweaks %}
<form method="post" class="form">
{% csrf_token %}
{% for hidden_field in form.hidden_fields %}
{{ hidden_field }}
{% endfor %}
{% if form.non_field_errors %}
<div class="alert alert-danger" role="alert">
{% for error in form.non_field_errors %}
{{ error }}
{% endfor %}
</div>
{% endif %}
{% for field in form.visible_fields %}
<div class="form-group">
{{ field.label_tag }}
{% if form.is_bound %}
{% if field.errors %}
{% render_field field class="form-control is-invalid" %}
{% for error in field.errors %}
<div class="invalid-feedback">
{{ error }}
</div>
{% endfor %}
{% else %}
{% render_field field class="form-control is-valid" %}
{% endif %}
{% else %}
{% render_field field class="form-control" %}
{% endif %}
{% if field.help_text %}
<small class="form-text text-muted">{{ field.help_text }}</small>
{% endif %}
</div>
{% endfor %}
<button type="submit" class="btn btn-primary">Submit</button>
</form>
So how do I get 3 of those forms on my page and be able to submit 3 different URLs?
I can only think of having to create 3 different form classes and paste the form snippet 3 times into the template. But that seems like a lot of unnecessary repetition.
Why "create 3 different form classes" ??? You can just create three instances of the same form.
paste the form snippet 3 times into the template
Ever heard about lists and loops ? You can put the three (or more) forms in a list and loop over it.
BUT that's actually not the best solution here - Django has Formsets for this use case.

csrf_token missing and how to get details for specific id entered by user

I am trying to display the result for the specific id entered by the user. I 'm not sure about the view.py file also. What changes should I need to make to get the desired result?
view.py file
def show(request):
if request.method == 'POST':
Num = allData.objects.only('emp_no')
data = request.POST.get('emp_no')
if data.is_valid():
for n in Num:
if n == data:
empid = data
emp = {'emp_no':data}
return render(request,'system/show.html',{'emp_no':data})
return(data.errors)
return HttpResponse("<h2>OOPS!! NO RECORD FOUND</h2>")
show.html
{% extends 'system/base.html' %}
{% load staticfiles %}
{% block body_block %}
<div class="container" "jumbotron">
<h2>Details</h2>
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
<label class="lb" for="emp_no" >Employee No.</label>
<input type="number" name="emp_no">
<button type="submit" class="btn btn-success">SUBMIT</button>
</form>
{% for allData in emp_no %}
{{ allData.GENDER_CHOICE}}
{{ allData.first_name }}
{{ allData.last_name }}
{{ allData.birth_day }}
{{ allData.hire_date }}
{{ allData.dept_no }}
{{ allData.dept_name }}
{{ allData.salary }}
{{ allData.from_date }}
{{ allData.to_date }}
{{ allData.titles }}
{% endfor %}
</div>
{% endblock %}
Welcome to SO!
From what you have there it looks like it would be worthwhile taking a look at the Django forms documentation.
The main thing that jumps out to me is that you are trying to reuse the same template both for the form and for the data display after the form is submitted. It might be easier to separate them.
If you do want to keep one template, then you don't want to show the form if there is data and vice versa - if there is no data, then you want to show the form. It would look something like this:
{% if emp_no %}
{% for allData in emp_no %}
{{ allData.GENDER_CHOICE}}
{{ allData.first_name }}
{{ allData.last_name }}
{{ allData.birth_day }}
{{ allData.hire_date }}
{{ allData.dept_no }}
{{ allData.dept_name }}
{{ allData.salary }}
{{ allData.from_date }}
{{ allData.to_date }}
{{ allData.titles }}
{% endfor %}
{% else %}
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
<label class="lb" for="emp_no" >Employee No.</label>
<input type="number" name="emp_no">
<button type="submit" class="btn btn-success">SUBMIT</button>
</form>
{% endif %}
The title of your question also mentions csrf_token problems. You didn't give any details, but my guess is things are getting confused because you are loading the form even when you don't need it.
I will point out a few things before trying to post a solution.
Your code is not that readable because of the way you name variables. Try naming your variables in a way that tries to reflect what your code is doing.
I will suggest that you finish django tutorial if you haven't done that already. It will help you grab the core concepts of Django.
From what I understood you are trying to get an employee with a given employee number.
This is how I would do it.
In the views.py I would user render instead of HttpResponse.
def show(request):
context = {}
if request.method == 'POST':
# extract the emp no from the request
emp_no = request.POST.get('emp_no')
# get or create the employee with this emp_no
current_employee = Employee.objects.get_or_create(emp_no)
context['current_employee'] = current_employee
return render(request, 'show.html', context)
In the templates, you don't need a forloop. You are retrieving only one employee.
{% extends 'system/base.html' %}
{% load staticfiles %}
{% block body_block %}
<div class="container" "jumbotron">
<h2>Details</h2>
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
<label class="lb" for="emp_no" >Employee No.</label>
<input type="number" name="emp_no">
<button type="submit" class="btn btn-success">SUBMIT</button>
</form>
<div>
{{ current_employee.GENDER_CHOICE}}
{{ current_employee.first_name }}
{{ current_employee.last_name }}
{{ current_employee.birth_day }}
{{ current_employee.hire_date }}
{{ current_employee.dept_no }}
{{ current_employee.dept_name }}
{{ current_employee.salary }}
{{ current_employee.from_date }}
{{ current_employee.to_date }}
{{ current_employee.titles }}
</div>
</div>
{% endblock %}

How to store form data in form pagination using session?

I'm working on a quiz. The quiz has multiple questions - forms. Since there may be many questions, I've created a pagination for these forms. Everything seems to work correctly but only the data from forms on last page are sent to server.
How to hold the data from previous pages so I can send all forms then to server?
I'm considering using session but can't figure out how to make it work. As you can see, all questions are in one form with method post. But pagination are GET methods.
def take_quiz(request,id):
if request.method == 'POST':
quiz = get_object_or_404(models.LanguageQuiz, pk=id)
sitting = get_object_or_404(models.Sitting, quiz=quiz, user=request.user)
for k,v in request.POST.iteritems():
if 'choice-for' in k:
q = Question.objects.get(pk=k.split('-')[-1])
choice = Choice.objects.get(pk=v)
sitting_question = get_object_or_404(SittingQuestion,sitting=sitting,question=q)
sitting_question.answer = choice
sitting_question.save()
correct_answers = len([x for x in sitting.sitting_questions.all() if x.answer.correct])
result = float(correct_answers)/sitting.sitting_questions.all().count() * 100
return render(request,'quiz/result.html',context={'result':result,
'level':level,
'quiz':quiz})
if request.method == 'GET':
with transaction.atomic():
quiz = get_object_or_404(models.LanguageQuiz, pk=id)
if models.Sitting.objects.filter(quiz=quiz, user=request.user).exists():
sitting = get_object_or_404(models.Sitting, quiz=quiz, user=request.user)
check_expired_sitting(sitting)
sitting.delete()
sitting = models.Sitting.objects.create(quiz=quiz, user=request.user)
sitting.load_questions()
questions = [x.question for x in sitting.sitting_questions.all()]
paginator = Paginator(questions,2)
page_num = request.GET.get('page',1)
page = paginator.page(page_num)
context = {'page':page,
'quiz':quiz}
return render(request,'quiz/quiz.html',context=context)
TEMPLATE
{% extends 'base.html' %}
{% block content %}
<h1>{{ quiz.name }}</h1>
<br><br><br>
<form action="" method="post">{% csrf_token %}
{% for question in page.object_list %}
<div class="question">
<h3>{{ question.text }}</h3>
<div class="choices">
{% for choice in question.get_choices_list %}
<input type="radio" name="choice-for-question-{{ question.id }}"
id="choice-{{ question.id }}-{{ forloop.counter }}" value="{{ choice.id }}"/>
<label for="choice-{{ question.id }}-{{ forloop.counter }}">{{ choice.text }}</label><br/>
{% endfor %}
</div>
</div>
{% endfor %}
{% if not page.has_next %}
<button class='accordion' type="submit">Submit Quiz</button>
{% endif %}
</form>
<div class="pagination">
<span class="step-links">
{% if page.has_previous %}
previous
{% endif %}
<span class="current">
Page {{ page.number }} of {{ page.paginator.num_pages }}.
</span>
{% if page.has_next %}
next
{% endif %}
</span>
</div>
{% endblock %}

Flask/Jinja2 form submit button not working

I've got a simple form where a user enters two dates. My input is getting passed correctly, but the Submit button isn't working. Here's my view:
# GLASS ------------------------------------------------------------------------
class NameForm(Form):
starts_on = StringField('Starts', validators=[Required()])
ends_on = StringField('Ends', validators=[Required()])
submit = SubmitField('Go')
#app.route('/glass/', methods = ['GET', 'POST'])
def glasses():
starts_on = None
ends_on = None
results = None
form = NameForm()
if form.validate_on_submit():
starts_on = form.starts_on.data
ends_on = form.ends_on.data
# SQL takes starts_on, and ends_on as inputs
results, start_date, end_date, companies_tracked = diagUserActs.userTime(starts_on, ends_on, diagUserActs.companies)
form.starts_on.data = ''
form.ends_on.data = ''
return render_template('glass.html', form = form, results = results)
And here's my template:
{% extends "base.html" %}
{% import "bootstrap/wtf.html" as wtf %}
<!--{% block title %}Glasses{% endblock %}-->
{% block content %}
<form class="form-inline" method="post" role="form">
{{ wtf.form_field(form.starts_on) }}
{{ wtf.form_field(form.ends_on) }}
{{ wtf.form_field(form.submit) }}
</form>
Oddly enough, this method below works, but I'd like to list the form elements individually so I've got more control regarding its presentation:
<form class="form-inline" method="post" role="form">
{% for field in form %}
{{ field.label }}
{{ field(placeholder="YYYY-MM-DD") }}
{% endfor %}
</form>
What I've done before is use something like this:
{% macro render_field(field, class='None') %}
<div class="pure-control-group">
{{ field.label(class_=class)|safe }}
{{ field(class_=class)|safe }}
{% if field.errors %}
<p class="errors">
{% for error in field.errors %}
<p>
{{ error }}
</p>
{% endfor %}
</p>
{% endif %}
</div>
{% endmacro %}
Then use it something like this:
{{ render_field(form.username) }}
Where form is something like this:
form = forms.Login(request.form)
It seems there is something wrong with the form here:
{{ wtf.form_field(form.starts_on) }}
I've never seen it used that way.