Dajngo inline formset and errors for required field - django

This is my model:
class dateEvent(models.Model):
venue = models.ForeignKey(Venue, on_delete=models.CASCADE)
event = models.ForeignKey('Event', on_delete=models.CASCADE)
start_date_time = models.DateTimeField(auto_now=False, auto_now_add=False)
My views.py:
def event_edit_view(request, id):
event = get_object_or_404(Event, id=id)
#MyOtherForm instantiated by event
DateEventFormSet = inlineformset_factory(Event, dateEvent, extra=5, can_delete=True, fields=('event', 'start_date_time', 'venue', 'link', 'link_description'),
widgets={
'venue': s2forms.Select2Widget(),
'start_date_time': CalendarWidget(),
form_date_event = DateEventFormSet(request.POST or None, instance=Event.objects.get(id=id), prefix="dateEvent", queryset=dateEvent.objects.filter(event__id=id))
if request.method == "POST":
if MyOtherForm.is_valid() and form_date_event.is_valid():
MyOtherForm.save()
form_date_event.save()
return redirect('my-events')
else:
raise forms.ValidationError(form_date_event.errors) #?
context = {
[other forms...]
'form_date_event': form_date_event,
}
return render(request, "events/template.html", context)
And my template.html:
<table id="dateEvent">
<thead>
<th>Venue</th>
<th>Date and time</th>
<th>Link</th>
<th>Link description</th>
</thead>
<tbody id='date_body'>
{{ form_date_event.management_form }}
{% for formDate in form_date_event.forms %}
<tr class="form-row-dateEvent" style='display:table-row;'>
<td>{{formDate.venue}}<br>
Add a new venue</td>
<td>{{ formDate.start_date_time}}</td>
<td>{{formDate.link}} {{formDate.this_composition}}</td>
<td>{{formDate.id}}{{formDate.link_description}}</td>
</tr>
{% endfor %}
</tbody>
</table>
Now, how can I enforce the user to fill in both venue and the date? As it is now if a user fills in the venue field and leaves the corresponding date field empty, Django redirects the user to an ugly yellow page
ValidationError at /private/event-edit/1150/
['This field is required.']
Request Method: POST
Request URL: .../event-edit/1150/
Django Version: 3.1
Exception Type: ValidationError
Exception Value:
['This field is required.']
Exception Location: /home/.../.../.../views.py, line 682, in event_edit_view
if DEBUG=TRUE is set, otherwise an even uglier Error 500. Is there a way that the user would get a nice 'please fill in this field' message in the same row next to the populated venue field, when they hit the 'submit' button?
PS
Do I need to declare a custom clear() method, according to which if a record has a venue needs also to have a date populated?

I think an issue is inside your event_edit_view function inside the views.py file, you should pass the formset in the context inside the POST method condition
do this in your else part instead of raising the validation error. It should work now.
def event_edit_view(request, id):
.....
if request.method == "POST":
.....
if MyOtherForm.is_valid() and form_date_event.is_valid():
MyOtherForm.save()
form_date_event.save()
return redirect('my-events')
else:
context = {
[other forms...]
'form_date_event': form_date_event,
}
return render(request, "events/template.html", context)

Related

How to create a record from request GET data (form does not pass the validation)

I am confused when I try to insert record from a "GET" request
I will try to explain what I want to do.
I am creating an application to take inventory of assets.
I have 3 tables in my database.
I have a main table called
fixed asset("ActFijo") where all the assets of my company are registered.
Another call Inventory ("Inventario"), which stores the name of each inventory
and another call Inventory_detail ("Inventario_detalle"), where the details or assets in which they are being counted are stored to verify that the equipament or furniture is not being stolen in that location.
From the main table ("ActFijo") I have to search for the furniture or asset and store it in the detail table ("Inventario_detalle")
I'm confused I don't know how to work on a GET request and then do a POST all in one request
Do I have to write my code in parts in a GET request and then POST?
Or can I do everything in the GET request?
This is the code I have so far
I don't know if it's ok, please I need guidance
For example my code does not pass the validation of the form.
if form.is_valid():
I am trying to print, But I don't see any validation error, it doesn't print anything
print(form.errors)
Views.py
from django.shortcuts import redirect, render
from .form import InventarioDetalle_Form, InventarioForm
from .models import ActFijo, Inventario, Inventario_detalle
# Create your views here.
def inventario_home_view(request):
if request.method == "GET":
inv = Inventario.objects.all()
context = {"inventarios": inv}
return render(request, "inventario/index.html", context)
def inventario_crear_view(request):
if request.method == "POST":
form = InventarioForm(request.POST)
if form.is_valid():
form.save()
return redirect("inventario-home")
else:
form = InventarioForm()
inv = Inventario.objects.all()
context = {"formulario": form, "inventarios": inv}
return render(request, 'inventario/crear.html', context)
def inventario_detalle_view(request, inventario):
if request.method == "GET":
# Obtener el valor del input "Buscar"
codigo_activo = request.GET.get("buscar")
print("[CODIGO ACTIVO]:", codigo_activo)
# Buscar el activo en la bd por el campo codigo
try:
activo = ActFijo.objects.get(codigo=codigo_activo)
# print(activo)
except ActFijo.DoesNotExist:
activo = None
if activo:
form = InventarioDetalle_Form(instance=activo)
# print(form)
print(form.errors)
if form.is_valid():
instance = form.save(commit=False)
instance.inventario_id = inventario
instance.save()
else:
print(
"This request does not pass the validation")
else:
print(
"The element does not exist")
context = {"item": Inventario_detalle.objects.all()}
return render(request, "inventario/detalle.html", context)
form.py:
from django import forms
from .models import Inventario, Inventario_detalle
class InventarioForm(forms.ModelForm):
class Meta:
model = Inventario
fields = '__all__'
class InventarioDetalle_Form(forms.ModelForm):
class Meta:
model = Inventario_detalle
fields = '__all__'
url.py
from django.urls import path
from django import views
from . import views
urlpatterns = [
path("", views.inventario_home_view, name="inventario-home"),
path("create/", views.inventario_crear_view,
name="inventario-create"),
path('detail/<int:inventario>',
views.inventario_detalle_view, name="inventario-detail"),
]
detail.html
{% extends "core/base.html" %} {% block content%}
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<div class="titulo mt-5">
<h1>Inventario Detalle</h1>
</div>
<form method="get">
<input type="text" class="form-control" placeholder="Buscar Activo" name="buscar" />
</form>
<div style="overflow-x: auto">
<table>
<thead>
<tr>
<th>Codigo</th>
<th>Descripcion</th>
<th>Accion</th>
</tr>
</thead>
<tbody>
{% for i in item %}
<tr>
<td>{{i.codigo}}</td>
<td>{{i.descripcion}}</td>
<td><button type="button" class="btn btn-danger">Eliminar</button></td>
</tr>
</tbody>
{% endfor %}
</tbody>
</table>
</div>
<div>{{request.GET}}</div>
</div>
</div>
</div>
{% endblock %}
The problem is here
def inventario_detalle_view(request, inventario):
if request.method == "GET":
codigo_activo = request.GET.get("buscar")
print("[CODIGO ACTIVO]:", codigo_activo)
try:
activo = ActFijo.objects.get(codigo=codigo_activo)
print(activo)
except ActFijo.DoesNotExist:
activo = None
if activo:
form = InventarioDetalle_Form(instance=activo)
print(form.errors)
if form.is_valid():
instance = form.save(commit=False)
instance.inventario_id = inventario
instance.save()
else:
print(
"This request does not pass the validation")
else:
print(
"The element does not exist")
context = {"item": Inventario_detalle.objects.all()}
return render(request, "inventario/detalle.html", context)
I believe you don't see any validation errors on the form because for the GET request, you are not passing in anything. The only thing you're passing into the form is the model instance and you're running form.is_valid on it which does not make sense. You dont need to use the form at all. Use this instead.
def inventario_detalle_view(request, inventario):
if request.method == "GET":
codigo_activo = request.GET.get("buscar")
print("[CODIGO ACTIVO]:", codigo_activo)
try:
activo = ActFijo.objects.get(codigo=codigo_activo) # get activo object
activo.inventario_id = inventario # update object
activo.save() # save changes
print(activo)
except ActFijo.DoesNotExist:
# you can do anthing here
# maybe redirect with a message..
pass
context = {"item": Inventario_detalle.objects.all()}
return render(request, "inventario/detalle.html", context)

Django form not calling custom validator

I'm using Django 2.0. This is my forms.py:
class PostcodeForm(forms.Form):
postcode = forms.CharField(required=True, widget=forms.TextInput(
attrs={
'placeholder': "enter a postcode",
}
))
def clean_postcode(self):
postcode = self.clean_data.get('postcode', '')
print('clean_postcode', postcode)
if postcode != 'something':
raise forms.ValidationError(_("Please enter a valid postcode"), code='invalid')
return data
And my views.py:
def index(request):
form = PostcodeForm()
context = {
'form': form
}
return render(request, 'index.html', context)
And my index.html:
<form class="form-inline" id="lookup_postcode" action="{% url 'lookup_postcode' %}" method="get">
{% csrf_token %}
{{ form.non_field_errors }}
{{ form.postcode.errors }}
{{ form.postcode }}
<button type="submit">Submit</button>
</form>
But when I type in any value other than 'something', the form still submits. I also don't see any print statements in the console, so it looks as though the validator just isn't being run.
What am I doing wrong?
At the moment you are always doing form = PostcodeForm(), for GET and POST requests. That means that the form is not bound to any data, so it will never be valid or have any errors.
In Django, a typical view to process a form looks something like this:
from django.shortcuts import redirect
def index(request):
if request.method == 'POST':
form = PostcodeForm(request.POST)
if form.is_valid():
# form is valid. Process form and redirect
...
return redirect('/success-url/')
else:
form = PostcodeForm()
context = {
'form': form
}
return render(request, 'index.html', context)
For this to work, you'll need to change your form method to 'post'.
<form class="form-inline" id="lookup_postcode" action="{% url 'lookup_postcode' %}" method="post">
If you keep the form method as 'get' then you'll need to bind the form to request.GET instead. You might want to add a check, otherwise you'll get errors for required fields when you first access the index view.
if 'postcode' in request.GET:
# bound form
form = PostcodeForm(request.GET)
else:
# unbound, empty form
form = PostcodeForm()
Use your form as below:
class PostcodeForm(forms.Form):
postcode = forms.CharField(required=True, widget=forms.TextInput(
attrs={
'placeholder': "enter a postcode",
}
))
def clean(self):
postcode = self.cleaned_data.get('postcode', '')
print('clean_postcode', postcode)
if postcode != 'something':
raise forms.ValidationError(_("Please enter a valid postcode"), code='invalid')
return super(PostcodeForm, self).clean()
Everytime you deal with the validity of the posted data, make sure to include form.is_valid() condition in your views.py.

Django template isn't rendering dynamic form errors

I have a Django 1.8 form that contains a paragraph tag that renders either some feedback or a question submitted by a user. It also contains a textarea input 'response_text' and a pair of radio buttons 'close_issue'. This response input can be used to send an optional response to the user. If the user submitted some feedback, the admin should be able to click the 'close issue' radio button and submit the form with no response. However, if the textarea input contains a question, then the form should render an error telling the admin that he/she can't submit the form without typing an answer into the response input. The problem I'm having is that I can't get the form to cause the template to render an error message if the user submitted a question but the admin didn't type in a response. My view, model, form, and template are shown below. forms.py shows all the ways (all commented out) I have tried to make the response input field required if the user submitted a question so that the template will display an error. I also tried overriding the default 'clean' method with one that would raise a ValidationError if the user submitted a question and the response input is blank but that didn't work either. Can anyone tell me what I'm doing wrong?
Thanks.
# view.py
def review_feedback_or_question(request, template, *args, **kwargs):
fqid = kwargs['fqid']## Heading ##
submission = FeedbackQuestion.objects.get(pk=fqid)
if request.method == 'POST':
form = FeedbackQuestionResponseForm(request.POST, submission=submission)
if form.is_valid():
# process the form
return redirect('review-feedback-or-question-queue')
else:
pass
form = FeedbackQuestionResponseForm(submission=submission)
context = {'form': form, 'submission': submission,}
return render(request, template, context)
# models.py
class FeedbackQuestion(models.Model):
SELECT = ''
FEEDBACK = 'feedback'
QUESTION = 'question'
SUBMISSION_TYPE_CHOICES = (
(SELECT , '-- Select --'),
(FEEDBACK, 'Feedback'),
(QUESTION, 'Question'),
)
user = models.ForeignKey(User, related_name="user")
submission_type = models.CharField(max_length=8,
choices=SUBMISSION_TYPE_CHOICES,
default=SELECT)
submission_text = models.TextField()
date_submitted = models.DateTimeField(auto_now_add=True)
response_text = models.TextField()
respondent = models.ForeignKey(User, related_name='respondent')
date_responded = models.DateTimeField(auto_now=True)
issue_closed = models.BooleanField(default=False)
class Meta:
db_table = 'feedback_question'
# forms.py
class FeedbackQuestionResponseForm(forms.Form):
TRUE = 1
FALSE = 0
BLANK = ''
CHOICES = ( (TRUE, 'Yes'), (FALSE, 'No') )
response_text = forms.CharField(
required=False,
label='',
widget=forms.Textarea(attrs={'placeholder': 'Enter response...'}))
close_issue = forms.TypedChoiceField(
choices=CHOICES,
label='Close this issue?',
widget=forms.RadioSelect(renderer=HorizontalRadioRenderer),
coerce=int)
def __init__(self, *args, **kwargs):
if 'submission' in kwargs:
submission = kwargs.pop('submission')
if submission.submission_type == 'question':
# NONE OF THESE WORKED!
#self.fields.get('response_text').required = True
#self.declared_fields['response_text'].required = self.TRUE
#self.declared_fields['response_text'].required = self.TRUE
#self.declared_fields['response_text'].required = True
#self._errors['response_text'] = "You must enter a response"
pass
super(FeedbackQuestionResponseForm, self).__init__(*args, **kwargs)
# template.html
<p>{{ submission.submission_text }}</p>
<form action="" method="post">{% csrf_token %}
{{ form.non_field_errors }}
{% if form.errors %}
{% if form.errors.items|length == 1 %}
Please correct the error below.
{% else %}
Please correct the errors below.
{% endif %}
</p>
{% endif %}
{{ form.response_text.errors }}
{{ form.response_text.label_tag }} {{ form.response_text }}
{{ form.close_issue.errors }}
{{ form.close_issue }} {{ form.close_issue.label_tag }}
<input type="submit" value="Submit" class="" />
</form>
You're not passing submission into the form when you instantiate it on POST, so the required attribute is never being set.
Daniel Roseman was correct in that I need to pass 'submission' into the form when I instantiate the form on POST. But there were still two other problems. First, I need to instantiate the form inside the else block. If this isn't done and the form doesn't validate, then you're passing an unbound form back to the viewer and any errors won't be displayed. Also, it isn't necessary to pass 'submission' to the form when you instantiate it here:
...
else:
form = FeedbackQuestionResponseForm()
context = {...}
...
The next problem was that the order of my statements inside the init method was incorrect. It appears that I needed to execute 'super()' before trying to reference the 'response_text' field. I'll need to locate and study this method in the Django source code to understand exactly why. In any case, this works:
def __init__(self, *args, **kwargs):
if 'submission' in kwargs:
submission = kwargs.pop('submission')
else:
submission = False
super(FeedbackQuestionResponseForm, self).__init__(*args, **kwargs)
if submission:
if submission.submission_type == 'question':
self.fields['response_text'].required = True
else:
self.fields['response_text'].required = False
When the above changes are implemented, the form will make the response_text field required if the user submits a question and an error will be displayed if the admin doesn't enter a response before submitting the form. Many thanks again to Daniel for getting me back on track towards finding a solution.

Generate choice field with existing ORM data in Django

I'm using Django 1.4 with Python 2.7 on Ubuntu 12.04.
I'm going to continue to update this question as I make progress.
UPDATE 2:
I'm trying to generate 2 choice fields in a form using information from an existing model.
class AssignProject(forms.Form):
def __init__(self, devs, *args, **kwargs):
"""
.. method:: __init__()
Class constructor
:param devs: Tuple with which developer
"""
super(AssignProject, self).__init__(*args, **kwargs)
self.dev = forms.ChoiceField(widget = forms.Select(), choices = devs, required = True)
self.designer = forms.ChoiceField(widget = forms.Select(), choices = devs, required = True)
At this point I can't seem to access dev and designer ChoiceField in my template yet.
Here is the view:
#login_required
def view_all_projects(request):
"""
.. function:: view_projects()
Show the projects
:param request: Django Request object
"""
data = { 'user' : request.user }
if (request.user.is_authenticated() and request.user.is_superuser):
all_projects = Projects.objects.filter(active = True)
dev_info = User.objects.filter(is_staff = True, is_superuser = False)
dev_dict = {}
for dev in dev_info:
dev_dict[dev.id] = '{0} {1}'.format(dev.first_name, dev.last_name)
devs = tuple(dev_dict.items())
form = AssignProject(devs)
data.update({ 'form' : form })
data.update({ 'projects' : all_projects })
data.update(csrf(request))
return render_to_response("view_all_projects.html", data)
return render_to_response("index.html", data)
I have verified that the developers/designers are properly getting set in the devs tuple.
...and the template from view_all_projects.html:
<form action="/assignProject/" method="post">{% csrf_token %}
<table>
<td>
<input type="hidden" name="project_id" value={{ project.id }}>
<tr>
<td align="right"><label class="formlabel">Assign Developer:<br /></label></td><td>{{ form.dev }}</td>
</tr>
<tr>
<td align="right"><label class="formlabel">Assign Designer:<br /></label></td><td>{{ form.designer }}</td>
</tr>
<tr>
<td align="right"><label class="formlabel"> </label></td><td><input type="submit" value="Submit ►"></td>
</tr>
</td>
</table>
</form>
I don't see any errors, but I do see a strange object reference in place of the ChoiceField in the template.
<django.forms.fields.ChoiceField object at 0x7ffdbc054190>
<django.forms.fields.ChoiceField object at 0x7ffdbc0542d0>
I see these instead. I know I'm close...just can't quite get what I'm going wrong.
Thoughts?
The issue was entirely in the form __init__.
I should have assigned the fields like follows:
class AssignProject(forms.Form):
def __init__(self, devs, *args, **kwargs):
"""
.. method:: __init__()
Class constructor
:param devs: Tuple with developers
"""
super(AssignProject, self).__init__(*args, **kwargs)
self.fields['dev'] = forms.ChoiceField(widget = forms.Select(), choices = devs, required = True)
self.fields['designer'] = forms.ChoiceField(widget = forms.Select(), choices = devs, required = True)
Note the self.fields['dev'] not self.dev. Fixed everything.

modelformset_factory does not honor extra parameter

Django: 1.4.1
Model:
class Hoja(models.Model):
nombre = models.CharField(max_length=200) # requerido
class Linea(models.Model):
hoja = models.ForeignKey(Hoja) # requerido
nombre = models.CharField(max_length=200) # requerido
padre = models.ForeignKey('self', null=True, blank=True, related_name='hijo')
View:
lineas = Linea.objects.filter(hoja=alt).order_by('id')
LineaHojaSet = modelformset_factory(Linea, can_delete=True, extra=1 if request.POST.has_key('siguiente') else 0)
formset = LineaHojaSet(request.POST or None, queryset=lineas)
if request.method=='POST':
# process formset
return render_to_response('template.html', {'formset':formset}, context_instance=RequestContext(request))
Template:
<table>
<thead>
<tr><th>Nombre</th><th>Borrar</th></tr>
</thead>
<tbody>
{% for fs in formset %}
<tr>
<td>{{ fs.nombre }}</td>
<td>{{ fs.id }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<input type="submit" name="siguiente" value="Añadir siguiente" />
When I submit the "siguiente" button, I can see than the formset is getting the correct extra field of 1, but in the webpage, the only rows showing are the database ones. It's this a bug, or I'm doing something wrong?
Formset factory finds number of forms either by max_num, extra parameters or form-TOTAL_FORMS parameter in request.POST (or data) from management form.
In your case, request.POST['form-TOTAL_FORMS'] has number which does not include extra form . So it does not add extra form when you create formset.
One solution would be to increment this number by one when your condition is met. e.g.
data = None
if request.POST:
data = request.POST.copy() #required as request.POST is immutable
if request.POST.has_key('siguiente'):
data['form-TOTAL_FORMS'] = int(data['form-TOTAL_FORMS']) + 1
#now use data instead of request.POST
formset = LineaHojaSet(data, queryset=lineas)
....
However, there are some drawbacks of manipulating formset this way. When you validate formset, the extra form will show errors if there are any required fields.
Better solution would be to create formset again before passing it template with one extra form and queryset. Most likely, when formset is valid, you would save any new objects, those will get added by queryset. So your page will show newly added objects and one extra form.
lineas = Linea.objects.filter(hoja=alt).order_by('id')
LineaHojaSet = modelformset_factory(Linea, can_delete=True,)
formset = LineaHojaSet(request.POST or None, queryset=lineas)
if request.method=='POST':
# process formset
if formset.is_valid:
#saved and done with formset.
if request.POST.has_key('siguiente'):
LineaHojaSet = modelformset_factory(Linea, can_delete=True, extra=1)
formset = LineaHojaSet(queryset=lineas)
...
return render_to_response('template.html', {'formset':formset}, context_instance=RequestContext(request))