So, i want to get the checked checkboxes items ids as a list and show them on another page. But when i get to that specific page i get the value 'None' instead of the list of ids. What could go wrong? I tried some different versions from another questions already posted on the site, but the result was the same.
Here is the code:
models.py:
from django.db import models
class afirmatii(models.Model):
text = models.CharField(max_length = 250)
def __str__(self):
return self.text
views.py:
def exam(request):
if request.method == 'POST':
checks = request.POST.get('selected[]')
request.session['checks2'] = checks
context = {
'title' : 'Title1',
'aff': afirmatii.objects.order_by('id')
}
return render(request, 'pages/exam.html', context)
def result(request):
checks = request.session.get('checks2')
context = {
'title' : 'Title2',
'checks': checks
}
return render(request, 'pages/result.html', context)
exam.html:
{% extends "./base.html" %}
{% block content %}
<div class="text-break">
<form action="{% url 'result' %}" method="POST">
{% csrf_token %}
{% for q in aff %}
<div class="border mb-3 rounded-sm bg-light p-2">
<div class="custom-control custom-checkbox checkbox-info">
<input type="checkbox" class="custom-control-input" id="{{ q.id }}" name = "selected[]">
<label class="custom-control-label" for="{{ q.id }}" name = 'selected[]'> {{ q.text }} </label>
</div>
</div>
{% endfor %}
<button type="submit" class="btn btn-md btn-outline-info">Next</button>
</form>
</div>
{% endblock content %}
result.html:
{% extends "./base.html" %}
{% block content %}
<body>
<div class="text-center">
<p class="pb-5"> {{ checks }} </p><br>
<div class="row">
<div class="col">
Home
</div>
<div class="col">
Learn more
</div>
</div>
</div>
</body>
{% endblock content %}
You can try use FORMSETS A formsets is a layer of abstraction to work with multiple forms on the same page.
You can paint the list of questions according to records in the table and get ids (or other fields) of the marked answers
forms.py
class ExamForm(forms.Form):
checkbox = forms.BooleanField(required=False)
id = forms.CharField(widget=forms.HiddenInput)
text = forms.CharField(widget=forms.HiddenInput)
views.py
from django.shortcuts import render
from django.forms import formset_factory
from .forms import *
def exam(request):
aff = afirmatii.objects.order_by('id')
exam_formset = formset_factory(ExamForm, extra=0)
formset = exam_formset(initial=[{'id': x.id, 'text': x.text} for x in aff])
checks = []
if request.method == 'POST':
formset = exam_formset(request.POST)
if formset.is_valid():
for form in formset.forms:
if form.cleaned_data.get('checkbox', None):
checks.append(form.cleaned_data)
context = {
'formset': formset,
'checks': checks,
}
return render(request, 'pages/exam.html', context)
exam.html
{% if not checks %}
<h1>Exam:</h1>
<form action="{% url 'exam' %}" method="POST">
{% csrf_token %}
{{ formset.management_form }}
{% for form in formset.forms %}
<div class="form-group">
{{ form.checkbox }}
{{ form.text.value }}
{{ form.id }}
{{ form.text }}
</div>
{% endfor %}
<button type="submit" class="btn btn-primary">Submit</button>
</form>
{% else %}
<h1>Results:</h1>
{% for check in checks %}
<p>{{ check.id }} {{ check.text }}</p>
{% endfor %}
{% endif %}
Related
I am using the following code in my django app which is working fine but i am getting this error when trying to save a form:
['ManagementForm data is missing or has been tampered with']
Views:
def employedit(request, pk, id):
employ_academic_forms = EmployAcademicUpdateFormSet(queryset=EmployAcademicInfo.objects.filter(employ_id=pk))
if request.method == 'POST':
employ_academic_forms = EmployAcademicUpdateFormSet(request.POST)
if employ_academic_forms.is_valid():
employ_academic_forms.save()
return redirect('employ-list')
context = {
'employ_academic_forms':employ_academic_forms,
}
return render(request, 'admins/employ/edit_employ.html', context)
form:
EmployAcademicUpdateFormSet = modelformset_factory(
EmployAcademicInfo,
exclude = ['employ_id'],
extra=0,
labels = {
'degree': 'Enter Employ Degree',
'last_passing_institution_name': 'Enter Employ Passing Institution',
'last_passing_year': 'Enter Employ Passing Year',
},
widgets = {
'degree' : forms.Select(attrs={'class':'form-control form-control-lg', 'placeholder':'Enter degree'}),
'last_passing_institution_name' : forms.TextInput(attrs={'class':'form-control form-control-lg', 'placeholder':'Enter institution name'}),
'last_passing_year' : forms.DateInput(attrs={'class':'form-control form-control-lg', 'type':'date'}),
},
)
Html:
{% extends 'base/base.html' %}
{% load static %}
{% load crispy_forms_tags %}
{% block content %}
<div class="card">
<form class="form-horizontal" action="" method="post">
{% csrf_token %}
<div class="card-body">
<div class="card-body">
<div class="form-horizontal">
{{ employAcademicFormSet.management_form }}
{% for form in employ_academic_forms %}
{% for field in form.visible_fields %}
<div class="form-group row">
<label class="col-md-3 col-form-label" for="text-input"><h6>{{ field.label_tag }}</h6></label>
<div class="col-md-9">{{ field }}</div>
</div>
{% endfor %}
{% endfor %}
</div>
</div>
</div>
<div class="card-footer">
<button class="btn btn-lg btn-primary" type="submit">Submit</button>
</div>
</form>
</div>
{% endblock %}
Does anyone know what is causing this? Please help me!!!
I think you might be using the wrong name when calling management_form.
Can you try replacing
{{ employAcademicFormSet.management_form }}
with
{{ employ_academic_forms.management_form }}
and see if that helps.
I have a page that contains a form. It has 3 buttons, Enter/Leave and Options. My enter and leave button operate just fine, but the options button is supposed to redirect to a list of entries and currently it does not do anything, not even produce errors, which I can't figure out why it's happening.
I feel like I'm missing something very slight, I tried moving the Manager Options button into the form tags but this did not work either, so I'm not sure I'm missing an important piece as I am fairly new to Python/Django.
views.py
class EnterExitArea(CreateView):
model = EmployeeWorkAreaLog
template_name = "operations/enter_exit_area.html"
form_class = WarehouseForm
def form_valid(self, form):
emp_num = form.cleaned_data['adp_number']
if 'enter_area' in self.request.POST:
form.save()
return HttpResponseRedirect(self.request.path_info)
elif 'leave_area' in self.request.POST:
form.save()
EmployeeWorkAreaLog.objects.filter(adp_number=emp_num).update(time_out=datetime.now())
return HttpResponseRedirect(self.request.path_info)
elif 'manager_options' in self.request.POST:
return redirect('enter_exit_area_manager_options_list')
class EnterExitAreaManagerOptionsList(ListView):
filter_form_class = EnterExitAreaManagerOptionsFilterForm
default_sort = "name"
template = "operations/list.html"
def get_initial_queryset(self):
return EmployeeWorkAreaLog.active.all()
def set_columns(self):
self.add_column(name='Employee #', field='adp_number')
self.add_column(name='Work Area', field='work_area')
self.add_column(name='Station', field='station_number')
urls.py
urlpatterns = [
url(r'enter-exit-area/$', EnterExitArea.as_view(), name='enter_exit_area'),
url(r'enter-exit-area-manager-options-list/$', EnterExitAreaManagerOptionsList.as_view(), name='enter_exit_area_manager_options_list'),
]
enter_exit_area.html
{% extends "base.html" %}
{% block main %}
<form id="warehouseForm" action="" method="POST" novalidate >
{% csrf_token %}
<div>
<div>
{{ form.adp_number.help_text }}
{{ form.adp_number }}
</div>
<div>
{{ form.work_area.help_text }}
{{ form.work_area }}
</div>
<div>
{{ form.station_number.help_text }}
{{ form.station_number }}
</div>
</div>
<div>
<div>
<button type="submit" name="enter_area" value="Enter">Enter Area</button>
<button type="submit" name="leave_area" value="Leave">Leave Area</button>
</div>
</div>
</form>
{% endblock main %}
{% block panel_footer %}
<div class="text-center">
<button type="submit" name="manager_options" value="Options">
Manager Options
</button>
</div>
{% endblock panel_footer %}
list.html
{% extends "base.html" %}
{% load core_tags staticfiles %}
{% block head %}
<script src="{% static "js/operations/enter_exit_area_manager_options_list.js" %}"></script>
{% endblock head %}
{% block main %}
{% include 'core/list_view/list.html' %}
{% endblock main %}
You option buttons is really link to another page so you should add it to your template like this. Replacing button-styles class with however you want your button to look.
<a href="{% url 'enter_exit_area_manager_options_list' %}" class="button-styles">
Manager Options
</a>
This is my working FormWizard that I made by following this and this
views.py
from django.shortcuts import render
from django.template import RequestContext
from django.http import HttpResponseRedirect
from formtools.wizard.views import SessionWizardView
# Create your views here.
def index(request):
return render(request, 'wizardApp/index.html')
class ContactWizard(SessionWizardView):
template_name = "wizardApp/contact_form.html"
def done(self, form_list, **kwargs):
process_form_data(form_list)
return HttpResponseRedirect('../home')
def process_form_data(form_list):
form_data = [form.cleaned_data for form in form_list]
print(form_data[0]['subject'])
print(form_data[0]['info1'])
print(form_data[0]['info2'])
print(form_data[1]['sender'])
print(form_data[1]['info1'])
print(form_data[1]['info2'])
print(form_data[2]['message'])
print(form_data[2]['info1'])
print(form_data[2]['info2'])
return form_data
urls.py
from django.conf.urls import url
from wizardApp import views
from wizardApp.forms import ContactForm1, ContactForm2, ContactForm3
from wizardApp.views import ContactWizard
app_name = 'wizardApp'
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^home/$', views.index, name='index'),
url(r'^admin/', admin.site.urls),
url(r'^contact/$', ContactWizard.as_view([ContactForm1, ContactForm2, ContactForm3])),
]
forms.py
from django import forms
class ContactForm1(forms.Form):
subject = forms.CharField(max_length=100)
info1 = forms.CharField(max_length=100)
info2 = forms.CharField(max_length=100)
class ContactForm2(forms.Form):
sender = forms.EmailField()
info1 = forms.CharField(max_length=100)
info2 = forms.CharField(max_length=100)
class ContactForm3(forms.Form):
info1 = forms.CharField(max_length=100)
info2 = forms.CharField(max_length=100)
message = forms.CharField(widget=forms.Textarea)
contact_form.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
{{ wizard.form.media }}
</head>
<body>
<p>Step {{ wizard.steps.step1 }} of {{ wizard.steps.count }}</p>
<form action="/contact/" method="post">{% csrf_token %}
<table>
{{ wizard.management_form }}
{% if wizard.form.forms %}
{{ wizard.form.management_form }}
{% for form in wizard.form.forms %}
{{ form }}
{% endfor %}
{% else %}
{{ wizard.form }}
{% endif %}
</table>
{% if wizard.steps.prev %}
<button name="wizard_goto_step" type="submit" value="{{ wizard.steps.first }}">first step</button>
<button name="wizard_goto_step" type="submit" value="{{ wizard.steps.prev }}">prev step</button>
{% endif %}
<input type="submit" value="submit"/>
</form>
</body>
</html>
I am having a lot of trouble understanding how customizing each step of the form works. There is very little help out there for this unfortunately. I saw this post about creating multiple templates and that kind of helps, but my main disconnect is on how i create those templates and how they are implemented in each step.
In a normal form i can do something like this
<form novalidate="novalidate" autocomplete="on" method="POST">
{% csrf_token %}
<div class="form-horizontal">
<div class="form-left">
{{form.first_name}}
{{form.first_name.errors}}
</div>
<div class="form-right">
{{form.last_name}}
{{form.last_name.errors}}
</div>
</div>
<div>
{{form.email}}
{{form.email.errors}}
</div>
<div>
<input type="submit" value="Submit">
</div>
</form>
How do i access each individual field? Where i can add in html and other bits to help with general styling? How should i go about making one of these for each steps? Should i basically be copy and pasting the html and everything into other "templates"? How do i call each template for each step?
Thanks!
Hope you figured it out. For the sake of anyone who runs into this, this is how I solved this. I replaced {{ wizard.form }} with a for loop to manually render the inputs:
<form action="/contact/" method="post">{% csrf_token %}
<table>
{{ wizard.management_form }}
{% if wizard.form.forms %}
{{ wizard.form.management_form }}
{% for form in wizard.form.forms %}
{{ form }}
{% endfor %}
{% else %}
{% for field in wizard.form %}
<label for="{{ field.id_for_label }}">{{ field.label }}</label>
{{ field }}
<span class="message">{{ field.errors }}</span>
{% endfor %}
{% endif %}
</table>
{% if wizard.steps.prev %}
<button name="wizard_goto_step" type="submit" value="{{ wizard.steps.first }}">first step</button>
<button name="wizard_goto_step" type="submit" value="{{ wizard.steps.prev }}">prev step</button>
{% endif %}
<input type="submit" value="submit"/>
You can create a template for each of your forms and then associate it to its corresponding form as described in the docs or if you want each form to use the same template, all you need to do is set your template_name attribute in your ContactWizard class:
class ContactWizard(SessionWizardView):
template_name = "contact_form.html"
I want to display the necessary field errors when the user who is populating the form does not enter valid information into the forms.
However, each I time I test the error script by clicking the submit button without populating any of the fields I get this error :
UnboundLocalError at /nesting/ local variable 'content' referenced
before assignment
How do I correct this?
This is my code :
veiws.py
from django.shortcuts import render, redirect
from django.views.generic import TemplateView
from nesting.forms import Identity_form
from nesting.models import Identity_unique
class Identity_view(TemplateView):
template_name = 'nesting/nesting.html'
def get(self, request):
form = Identity_form()
Identities = Identity_unique.objects.filter(user = request.user)
var = {'form': form, 'Identities': Identities}
return render(request, self.template_name, var)
def post(self, request):
form = Identity_form(request.POST or None)
if form.is_valid():
NIS = form.save(commit = False)
NIS.user = request.user
NIS.save()
content = form.cleaned_data['NIS']
form = Identity_form()
return redirect('nesting:nesting')
var = {'form': form, 'content': content}
return render(request,self.template_name, var)
The error messages is saying the error is caused by this line
var = {'form': form, 'content': content}
return render(request,self.template_name, var)
nesting.html
{% extends 'base.html' %}
{% load widget_tweaks %}
<html>
<head>
{% block head %}
<title>nesting</title>
{% endblock %}
</head>
<body>
{% block body %}
<div class = "container" style = "margin-top: 80px;">
<form method = 'post' novalidate>
{% csrf_token %}
{% for hidden_field in form.hidden_fields %}
{{ hidden_field }}
{% endfor %}
<div class = "col-sm-8 col-md-8 col-lg-6">
<p class = "font-weight-bold">Create Patient</p>
{% 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">
{% 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 class = "btn-primary btn-large btn ">Submit</button>
</div>
</form>
</div>
<div class = "col-sm-8 col-md-6 col-lg-6">
{% for Identity in Identities %}
<div class = "card" style = "margin-top: 40px;">
<div class = "card-header">
<p class="font-weight-bold"> {{Identity.First_Name}} {{Identity.Last_Name}} </p>
</div>
<div class = "card-body">
<div class = "card-title">
<p class = "font-weight-light" style = "font-family: Optima">National Insurance Scheme : {{ Identity.NIS }}</p>
</div>
<p><small class = "card-text">Created On : {{ Identity.Timestamp }}</small></p>
<p><small class = "card-text">Address : {{ Identity.Residence }}</small></p>
</div>
</div>
{% endfor %}
</div>
</div>
{% endblock %}
</body>
</html>
In your views. content is referenced in var = {'form': form, 'content': content}. It is only instantiated after if form.is_valid() block.
The unboundError occurs when your form is invalid, then the var = {'form': form, 'content': content} does not have any variable named content to refer to.
The Fix: instantiate content before the if form.is_valid() block.
content = None
In a Django project, I have a view (cloud), type: CreateView. This view has a inlineformset_factory. It works. But, if i submit the form with a error (look at "messages.error" below), the page is redirected to project.get_absolute_url(). The problem is: the form content back empty. I know thats because the HttpResponseRedirect.
How can I change this without to break the form?
views.py
class cloud(CreateView):
template_name = 'base/cloud.html'
form_class = UserForm
def get_context_data(self, **kwargs):
context = super(cloud, self).get_context_data(**kwargs)
project = get_object_or_404(Project, slug=self.kwargs['slug'])
context['project'] = project
if self.request.POST:
context['formset'] = IdeaFormset(self.request.POST or None)
else:
context['formset'] = IdeaFormset()
return context
def form_valid(self, form, **kwargs):
project = get_object_or_404(Project, slug=self.kwargs['slug'])
context = self.get_context_data()
formset = context['formset']
if formset.is_valid():
self.object = form.save()
formset.instance = self.object #IdeaFormFormSet
nouns = project.nouns().values_list('content', flat=True)
verbs = project.verbs().values_list('content', flat=True)
error = False
for form in formset.forms: #For each Idea
form.instance.project = project
if form.instance.sentence:
sentence = form.instance.sentence
validate_noun = [word for word in sentence.lower().split() if word in nouns]
validate_verbs = [word for word in sentence.lower().split() if word in verbs]
if (len(validate_noun) < 1):
error = True
messages.error(self.request, u'No noun was inserted into the sentence.', 'danger')
if (len(validate_verbs) < 1):
error = True
messages.error(self.request, u'No verb was inserted into the sentence.', 'danger')
if not error:
formset.save()
messages.success(self.request, u'Success!')
return HttpResponseRedirect( project.get_absolute_url() )
else:
return self.render_to_response(self.get_context_data(form=form))
cloud.html
<form role="form" method="post">
{% csrf_token %}
<legend>Ideas</legend>
<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
{% for idea_form in formset %}
{# Include the hidden fields #}
{% for hidden in idea_form.hidden_fields %}
{{ hidden }}
{% endfor %}
<div class="panel panel-default">
<div class="panel-heading" role="tab" id="heading_{{ forloop.counter }}">
<h4 class="panel-title">
<a {% if not forloop.first %} class="collapsed" {% endif %}
data-toggle="collapse" data-parent="#accordion" href="#collapse_{{ forloop.counter }}" aria-expanded="{{ forloop.first }}" aria-controls="collapse_{{ forloop.counter }}">
Idea #{{ forloop.counter }}
</a>
</h4>
</div>
<div id="collapse_{{ forloop.counter }}" class="panel-collapse collapse {% if forloop.first %} in {% endif %}" role="tabpanel" aria-labelledby="heading_{{ forloop.counter }}">
<div class="panel-body form-group" id="idea_{{ forloop.counter }}">
<div class="container-fluid">
{% for field in idea_form.visible_fields %}
<div class="row">
{{ field.errors }}
{{ field.label_tag }} {{ field }}
<span class="help-block">{{ field.help_text }}</span>
</div>
{% endfor %}
</div> <!--container-fluid-->
</div>
</div>
</div>
{% endfor %}
</div>
<legend>User</legend>
{% for field in form %}
<div class="form-group has-error">
{{ field.label_tag }} {{ field }}
{% if field.help_text %}
<p class="help-inline"><small>{{ field.help_text }}</small></p>
{% endif %}
<div class="help-block with-errors">
<span class="help-block">
{% for error in field.errors %}{{ error }}{% endfor %}
</span>
</div>
</div>
{% endfor %}
{{ formset.management_form }}
<div class="form-actions">
<button type="submit" class="btn btn-primary">Send!</button>
</div>
</form>
As a general rule, model validation should go into model fields validators or model's clean method. Form validation should go into form's clean or clean_<field> methods.
If you need to validate things in your view's form_valid, you can use form.add_error and then, instead of redirecting (or returning super(cloud, self).form_valid(form), which redirects anyways), you could return super(cloud, self).form_invalid(form).
Check:
https://docs.djangoproject.com/en/3.1/ref/forms/api/#django.forms.Form.add_error