Django forms is not valid - python

my page html :
resrver salle !
<form>
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="reserver">
</form>
views .py :
def reserversalle(request , id):
form= ReserverSalle(request.POST or None)
print(form)
if form.is_valid():
print("gooddd")
context= {
'form' : form ,
}
return render(request,'registration/reserversalle.html', context)
forms.py :
class ReserverSalle(forms.Form):
nomsalle = forms.CharField(required=True , widget=forms.TextInput)
motifresa = forms.CharField(required=True , widget=forms.TextInput)
datedebut = forms.DateField( initial="2019-06-21",
widget=forms.SelectDateWidget(years=YEARS))
heuredebut = forms.TimeField( initial='00:00:00')
heurefin = forms.TimeField( initial='00:00:00')
hello i try to submit my form but my form is not valid please i need some help

Try adding form attributes action and method
<form action="." method="post">...</form>
if request.method == 'POST':
form = ReserverSalle(request.POST)
....
else:
form = ReserverSalle()

you need to specify method and action in your form tag in html
<form method="POST" action="<<URL_TO_HANDLE_YOUR_FORM>>">
<> means Url specified in your URL.py which direct it to the views.py function you have written

Related

Python Django Form "This field is required"

i made a form in django with one field required (imagefield) but when I fill it by selecting an image, once I fill it it considers that the form is not valid and asks to fill it again I don't understand why, here is my code below:
view.py:
def success(request):
return HttpResponse('successfully uploaded')
def contact(request, plage_name):
name = plage_name
plage = Spot.objects.get(name__iexact=name)
if request.method == 'POST':
form = ContactUsForm(request.FILES) # ajout d’un nouveau formulaire ici
if form.is_valid():
print("it's valide")
plage.photo = form.cleaned_data['photo']
plage.save()
return redirect('success')
else:
form = ContactUsForm()
return render(request,
'pages/Testform.html',
{'form': form}) # passe ce formulaire au gabarit
form.py
class ContactUsForm(forms.Form):
photo = forms.ImageField(required=True)
html
<form action="" method="post" novalidate>
{% csrf_token %}
{{ form }}
<input type="submit" value="Envoyer">
</form>
Url.py
urlpatterns = [
path('contact-us/<path:plage_name>', views.contact, name='contact'),
]
in views.py
if request.method == 'POST':
form = ContactUsForm(request.POST,request.FILES)
in HTML
<form action="" method="post" novalidate enctype="multipart/form-data">
{% csrf_token %}
{{ form }}
<input type="submit" value="Envoyer">
</form>
I think your error is on the parameters of form init. The first paramters is for POST data, not FILES:
form = ContactUsForm(request.POST, request.FILES)
And after, you have to call form.save(), probably better than make the update yourself...

ModelForm with multiple values not responsive

so I encountered this problem with Django and ModelForms. Everything loads as expected but when I'm trying to send data by hitting Enter nothing happens.
models.py
class Drinks(models.Model):
name = models.CharField(max_length=50)
number = models.DecimalField(decimal_places=2, max_digits=2000)
def __str__(self):
return self.name
forms.py ( I tried with list and tuple as well )
class DrinksForm(forms.ModelForm):
class Meta:
model = Drinks
fields = [
'name',
'number'
]
views.py
def DrinksView(request):
form = DrinksForm(request.POST or None)
if form.is_valid():
print("VALIDATION COMPLETE")
form.save()
form = DrinksForm()
return render (request, 'form2.html', { 'form' : form })
template.html
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
</form>
admin.py
from django.contrib import admin
from .models import Drinks
admin.site.register(Drinks)
I did all necessary migrations.
Any Ideas what im doing wrong?
Your form doesn't have a submit button:
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" />
</form>
For your view, consider this instead:
def new_drink_view(request):
if request.method == "POST":
form = DrinksForm(request.POST)
# check if valid
# ...
else:
form = DrinksForm()
return render (request, 'form2.html', { 'form' : form })
Be sure to import the DrinksForm form.

[DJANGO]: How to pass a Django form field value to a template form action?

I have a Django form that asks for id number. Hence, when the user clicks on submit, that id number is passed as a parameter to the endpoint.
This URL path('verify/nin/', views.post_nin, name='post_nin') contains the form and asks for the id number while I am submitting the data to this URL to
path('nin/<str:nin>',
views.nin_verification_vw, name="nin_verification")
So I expect to be redirected to http://127.0.0.1:8000/api/nin/15374020766 but instead it is redirecting me to http://127.0.0.1:8000/api/nin/%3Cinput%20type=%22text%22%20name=%22nin%22%20required%20id=%22id_nin%22%3E?nin=15374020766&csrfmiddlewaretoken=u5UmwDW4KRUIvYWXAa64J8g1dTPoJ3yDqtoCuKjboIE2TNxI3tPbjPmCK6FztVwW
How do I avoid the unnecessary parameters?
Here is my forms.py:
class NINPostForm(forms.Form):
"""Form for a user to verify NIN"""
nin = forms.CharField(required=True, help_text='e.g. 123xxxxxxxx')
# check if the nin is a valid one
def clean_nin(self):
nin = self.cleaned_data['nin']
regex = re.compile("^[0-9]{11}$")
if not regex.match(nin):
raise forms.ValidationError("NIN is incorrect.")
return nin
Here is my views.py:
def post_nin(request):
submitted = False
if request.method == 'POST':
form = NINPostForm(request.POST)
if form.is_valid():
cd = form.cleaned_data['nin']
return HttpResponseRedirect('/verify/nin?submitted=True')
else:
form = NINPostForm()
context = {
'form': form,
# 'cd': cd,
}
return render(request, 'ninform.html', context)
And here is my HTML template:
<form action="{% url 'nin_verification' form.nin %}" method="POST">
<table>
{{ form.as_table }}
<tr>
<td><input type="submit" value="Submit"></td>
</tr>
</table>
{% csrf_token %}
</form>
first import redirect : from django.shortcuts import redirect
Change your view to :
<form action="{% url 'post_nin' %}" method="POST">
<table>
{{ form.as_table }}
<tr>
<td><input type="submit" value="Submit"></td>
</tr>
</table>
{% csrf_token %}
</form>
You were passing the whole field instead of a String by using form.nin in your form action you should use your post_nin view to parse the nin field so...
Change your view to :
def post_nin(request):
submitted = False # Don't understand this part
if request.method == 'POST':
form = NINPostForm(request.POST)
if form.is_valid():
nin = form.cleaned_data['nin']
return redirect('nin_verification', nin=nin)
else:
form = NINPostForm()
context = {
'form': form,
}
return render(request, 'ninform.html', context)
`

Django, How to add two form in one page?

I am new to django, my question is simple. How can I add two form in the same page? I tried many things as making a class in views or add a second urls path but didn't find how. Thanks you for helping
this is my code:
forms.py
class scrap_info(forms.Form):
url = forms.CharField(label="Urls")
website = forms.ChoiceField(label="Website", choices=ask_website)
class sms_info(forms.Form):
data = forms.ChoiceField(label="Data list", choices=ask_data)
number = forms.CharField(label="Sms number")
views.py
def scrap_app(request):
form1 = scrap_info(request.POST or None)
return render(request, "sms/scrap_app.html", {'form1': form1})
def sms_app(request):
form2 = sms_info(request.POST or None)
return render(request, "sms/sms_app.html", {"form2": form2})
scrap_app.html
<body>
<div>
<form method="POST">
{% csrf_token %}
{{ form|crispy }}
<button class="btn btn-outline-info" type="submit" value="Save">SCRAP</button>
</form>
</div>
</body>
urls.py
urlpatterns = [
path("/scrap_app", views.scrap_app, name="scrap_app"),
]
I have encountered this problem just recently and I solved it by adding a hidden field on every form and getting that hidden value to determine what form was submitted by using an if condition in views
Here's how I did it using CBV.
views.py
class ContactUsView(TemplateView):
template_name = 'yourtemplate.html'
def get(self, request, *args, **kwargs):
inquiry_form = InquiryForm(self.request.GET or None, prefix='inquiry_form')
complaint_form = ComplaintForm(self.request.GET or None, prefix='complaint_form')
context = self.get_context_data(**kwargs)
context['complaint_form'] = complaint_form
context['inquiry_form'] = inquiry_form
return self.render_to_response(context)
def post(self, request):
# instantiate all unique forms (using prefix) as unbound
inquiry_form = InquiryForm(prefix='inquiry_form')
complaint_form = ComplaintForm(prefix='complaint_form')
# determine which form is submitting (based on hidden input called 'action')
action = self.request.POST['action']
# bind to POST and process the correct form
if action == 'inquiry':
inquiry_form = InquiryForm(data=request.POST, prefix='inquiry_form')
if inquiry_form.is_valid():
# Your logic here
return self.render_to_response(
self.get_context_data(
inquiry_form=inquiry_form,
complaint_form=complaint_form,
)
)
messages.error(self.request,
'Inquiry form is invalid.')
elif action == 'complaint':
complaint_form = ComplaintForm(data=request.POST, prefix='complaint_form')
if complaint_form.is_valid():
# Your logic here
return self.render_to_response(
self.get_context_data(
inquiry_form=inquiry_form,
complaint_form=complaint_form,
)
)
messages.error(self.request,
'Complaint form is invalid.')
# prep context
context = {
'inquiry_form': inquiry_form,
'complaint_form': complaint_form,
}
return render(request, self.template_name, context)
yourtemplate.html
<!-- First Form -->
<form action="" method="post" role="form">
{% csrf_token %}
<input type='hidden' name='action' value='inquiry'>
{{ form1 }}
<button type="submit" title="Send Inquiry">Send Inquiry</button>
</form>
<!-- Second Form -->
<form action="" method="post" role="form">
{% csrf_token %}
<input type='hidden' name='action' value='complaint'>
{{ form2 }}
<button type="submit" title="Send Complaint">Send Complaint</button>
</form>
As you can see there's a hidden value in every form named 'action', that will be the one to determine which form was submitted.

Django ModelForm error message not showing in template

I have created some custom error message following the documentation (as best as I can find) but I'm not getting any errors, let alone my custom errors. Here's my code:
forms.py
class UploadFileForm(forms.ModelForm):
class Meta:
model = Job
fields = ['jobID','original_file']
labels = {
'jobID': _('Job ID'),
'original_file': _('File'),
}
error_messages = {
'jobID': {
'max_length': _("Job ID is limited to 50 characters."),
'required': _("Please provide a Job ID."),
'invalid': _("Job ID must be a combination of letters, numbers, - and _.")
},
'original_file': {
'required': _("Please provide a file."),
'validators': _("Please ensure you are selecting a zipped (.zip) GDB."),
},
}
help_texts = {
'original_file': _('Please provide a zipped (.zip) GDB.'),
}
upload.html
<form method = "POST" action="{% url 'precheck:upload' %}" enctype="multipart/form-data" name="uploadForm">
{% csrf_token %}
{% for field in form %}
<div>
<strong>{{ field.errors }}</strong>
{{ field.label_tag }} {{ field }}
{% if field.help_text %}
<p class ="help-text">{{ field.help_text }}</p>
{% endif %}
</div>
{% endfor %}
<br />
<button type="button" id="uploadButton" data-loading-text="Loading..." class="btn btn-primary" autocomplete="off" style="margin: auto 20%; ">Upload</button>
</form>
views.py
def upload(request):
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES, user = request.user)
if form.is_valid():
form.save()
request.session['jobID'] = request.POST['jobID']
#job = Job.objects.filter(user_id = request.user.id).filter(jobID = request.POST['jobID']).latest()
# initialize(job)
return render(request,'precheck/run_precheck.html')
form = UploadFileForm()
historyList = Job.objects.filter(user_id = request.user.id)[:10]
return render(request, 'precheck/upload.html',{'form': form, 'history': historyList})
I've included everything I think is relevant, let me know if you need anything more.
The problem is that if the form is not valid, you're resetting the form to the initial form:
form = UploadFileForm()
historyList = Job.objects.filter(user_id = request.user.id)[:10]
return render(request, 'precheck/upload.html',{'form': form, 'history': historyList})
Your flow should render the bound form (with its errors) so it should be:
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES, user = request.user)
if form.is_valid():
# do stuff for valid form
return redirect
elif request.method == 'GET':
form = UploadFileForm()
# flow common for GET and invalid form
return render(request, template, {'form': form})

Categories