Django Forms Invalid but no Errors - django

Maybe it might be an oversight but I do not know the point where I am getting it wrong.
My form is rendered correctly but it keeps failing without errors.
forms.py
from crispy_forms.helper import FormHelper
from django import forms
from django.utils.translation import ugettext as _
class BeneficiaryForm(forms.Form):
"""Add Beneficiary template form"""
# Form fields
account_currency = forms.ModelChoiceField(queryset=Currency.objects.all(), empty_label=_('Select account currency'))
bank_account_type = forms.CharField(max_length=50, required=False)
email = forms.CharField(max_length=150, required=False, help_text=_("We'll notify them when a transfer is made"))
name = forms.CharField(max_length=50, required=False)
swift_code = forms.CharField(max_length=11, required=False,
widget=forms.TextInput(attrs={'placeholder': 'MSBCCNBJ001'}))
iban = forms.CharField(max_length=34)
def __init__(self, *args, **kwargs):
super(BeneficiaryForm, self).__init__()
self.helper = FormHelper()
self.helper.form_show_labels = False
views.py
def beneficiaries(request):
"""View function for viewing Beneficiaries and adding a Beneficiary instance"""
if request.method == 'POST':
form = BeneficiaryForm(request.POST)
if form.is_valid():
print("Form is valid")
print(request.POST['bank_account_type'])
print(request.POST['email'])
print(request.POST['name'])
print(request.POST['iban'])
print(request.POST['swift_code'])
print("Form is invalid")
print(form.errors)
form = BeneficiaryForm()
context = {
'form': form
}
return render(request, 'dashboard/beneficiaries.html', context)
and in my rendered form. I have this block to show errors and nothing shows up
HTML
<form action="{% url 'beneficiary_index' %}" method="post">
{% csrf_token %}
{% if form.errors %}
{% for field in form %}
{% for error in field.errors %}
<div class="alert alert-danger">
{% if field != '__all__' %}
<strong>{{ field.label }}:</strong>
{% endif %}
{{ error|escape}}
</div>
{% endfor %}
{% endfor %}
{% endif %}
<div class="form-group">
<div class="form-label-group">
<label class="form-label" for="default-01">{% trans "Account currency" %}</label>'
</div>
{{ form.account_currency | as_crispy_field }}
</div>
<div class="form-group">
<div class="form-label-group">
<label class="form-label" for="default-01">{% trans "Bank Account type" %}</label>
</div>
{{ form.bank_account_type | as_crispy_field }}
</div>
<div class="form-group">
<div class="form-label-group">
<label class="form-label" for="default-01">Their email(optional)</label>
</div>
{{ form.email | as_crispy_field }}
</div>
<div class="form-group">
<div class="form-label-group">
<label class="form-label" for="default-01">Full name of account holder</label>
</div>
{{ form.name | as_crispy_field }}
</div>
<h6>{% trans "Recipient Bank Information" %}</h6>
<hr>
<div class="form-group">
<div class="form-label-group">
<label class="form-label" for="default-01">Swift code</label>
</div>
{{ form.swift_code | as_crispy_field }}
</div>
<div class="form-group">
<div class="form-label-group">
<label class="form-label" for="default-01">IBAN</label>
</div>
{{ form.iban | as_crispy_field }}
</div>
<div class="form-group">
<button class="btn btn-lg btn-primary btn-block">{% trans 'Add Beneficiary' %}</button>
</div>
</form>
This is the Html to the form. I have just tried all the suggestions and still without the errors.
Form is Invalid is the only thing printed on screen without the form.errors

You need to create the GET request empty form in the else block or return the request in the POST block.
You are re-creating an empty form before returning the response.
def beneficiaries(request):
"""View function for viewing Beneficiaries and adding a Beneficiary instance"""
if request.method == 'POST':
form = BeneficiaryForm(request.POST)
if form.is_valid():
print("Form is valid")
print(request.POST['bank_account_type'])
print(request.POST['email'])
print(request.POST['name'])
print(request.POST['iban'])
print(request.POST['swift_code'])
# do other stuff like saving to DB
# then redirect
else:
print("Form is invalid")
print(form.errors)
elif request.method == 'GET':
form = BeneficiaryForm()
else:
# maybe return 404
pass
context = {
'form': form
}
return render(request, 'dashboard/beneficiaries.html', context)
Or maybe you can try Class Based Views, to avoid the if-else block for GET and POST requests.

Try adding this to the place you want the error to appear (In the HTML file)
Better if you use it inside the tags.
{% if form.errors %}
<p>Form is invalid</p>
{% endif %}
I Hope this answers the question.

Related

Django Recaptcha fails the form validation

I'm implementing RecaptchaV3 on a Django application.
I am currently following this github(https://github.com/kbytesys/django-recaptcha3) README text to set it up.
I have a feedback form that sends an email with the subject and text.
However, when I add RecaptchaField in django form, the POST request no longer passes the form.is_valid() in views.py.
Does anyone have a thought/fix on how to overcome this problem?
Thank you in advance
forms.py
class contactForm(forms.Form):
name = forms.CharField(max_length=50, required=True, widget=forms.TextInput(attrs={'placeholder': 'Name (required)'}))
email = forms.EmailField(max_length=100, required=True, widget=forms.TextInput(attrs={'placeholder': 'Email (required)'}))
subject = forms.CharField(max_length=200, widget=forms.TextInput(attrs={'placeholder': 'Subject'}))
message= forms.CharField(max_length=1000, widget=forms.Textarea(attrs={'placeholder': 'Message'}))
captcha = ReCaptchaField()
views.py
def homepage(request):
form = contactForm()
if request.method == 'POST':
form = contactForm(request.POST)
if form.is_valid():
print("form is valid")
form = contactForm()
return render(request, 'homepage/home.html', {'form':form, 'context':context,})
template
<form class="form-horizontal" id="contactForm" method="POST" action="?">
{% csrf_token %}
<div class="form-group">
<div class="form-group">
<label for="name">Your Name (required)</label><br>
{{ form.name }}
</div>
<div class="form-group">
<label for="email">Your Email (required)</label><br>
{{ form.email }}
</div>
<div class="form-group">
<label for="subject">Subject</label><br>
{{ form.subject }}
</div>
<div class="form-group">
<label for="message">Your Message</label><br>
{{ form.message }}
</div>
</div>
<button class="btn" type="submit">SEND</button>
</form>
in the header, i've added
{% recaptcha_init %}
{% recaptcha_ready action_name='Homepage' %}

Django contact form doesnt submit

I am trying to set up a contact form. I have implemented the Django-crispy-forms and now my form is not submitted (I don't have any errors).
I've added action="" to my form in my template without any success.
forms.py
class ContactForm(forms.Form):
name = forms.CharField(max_length=100, help_text='Enter your name or username')
email = forms.EmailField()
message = forms.CharField(widget=forms.Textarea(attrs={'rows': 3, 'cols': 40}), help_text='Example: I forgot my password!')
views.py
def contact_us(request):
if request.method == 'POST':
form = ContactForm(request.POST)
if form.is_valid():
sender_name = form.cleaned_data['name']
sender_email = form.cleaned_data['email']
message = "From {0}:\n\n{1}".format(sender_name, form.cleaned_data['message'])
send_mail('PLM_Tool contact', message, sender_email, ['myadress#gmail.com'])
return redirect('home:index')
else:
form = ContactForm()
return render(request, 'accounts/contact.html', {'form': form})
urls.py
app_name = 'accounts'
urlpatterns = [path('contact/', views.contact_us, name='contact'),]
contact.html
{% extends "base.html" %}
{% load crispy_forms_tags %}
{% block main %}
<form method="post" action="">
{% csrf_token %}
<div class="row">
<div class="col-6">
{{ form.name|as_crispy_field }}
</div>
<div class="col-6">
{{ form.email|as_crispy_field }}
</div>
<div class="col-6">
{{ form.message|as_crispy_field }}
</div>
</div>
</form>
<button type="submit" class="btn btn-success">Send</button>
Cancel
<br><br>
{% endblock %}
Here is the problem, and do not give action to form
crispy forms create the field not the button.
<form method="post">
{% csrf_token %}
<div class="row">
<div class="col-6">
{{ form.name|as_crispy_field }}
</div>
<div class="col-6">
{{ form.email|as_crispy_field }}
</div>
<div class="col-6">
{{ form.message|as_crispy_field }}
</div>
</div>
<button type="submit" class="btn btn-success">Send</button>
</form>
just add the button inside the form

Django edit view does not show

I am new to Django.
My app allows a user to create a project by providing a title and a body content, delete it and update it.
Now, I am creating and edit/update view for my app but it is not showing up in my html page.
urls.py
from django.urls import path, include
from . import views
urlpatterns = [
path('allprojects', views.allprojects, name='allprojects'),
path('createproject', views.createproject, name='createproject'),
path('<int:project_id>', views.projectdetail, name='projectdetail'),
path('<int:project_id>/editproject', views.editproject, name='editproject'),
]
projects/views.py
#login_required
def editproject(request, project_id):
if request.method == 'POST':
if request.POST['title'] and request.POST['content']:
project = get_object_or_404(Project, pk=project_id)
project.title = request.POST['title']
project.content = request.POST['content']
project.developer = request.user
project.save()
return redirect('/projects/' + str(project.id))
else:
return render(request, 'projects/editproject.html', {'error':'All fields are required.'})
else:
return render(request, 'projects/allprojects.html')
projects/templates/projects/editproject.html
{% extends 'base.html' %}
{% block title %}Edit Project{% endblock %}
{% block content %}
<div class="container">
<div class="row">
<div class="mt-4 offset-md-3 col-md-6">
<h2>Create a new project</h2>
<form method="post">
{% csrf_token %}
<div class="form-group">
<label for="exampleFormControlTextarea1">Title of the project</label>
<textarea class="form-control" id="exampleFormControlTextarea1" rows="1" name="title"></textarea>
</div>
<div class="form-group">
<label for="exampleFormControlTextarea1">Brief description of this project</label>
<textarea class="form-control" id="exampleFormControlTextarea1" rows="5" name="content"></textarea>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
</div>
{% endblock %}
PROBLEM
When I go to a urls such as http://localhost:8000/projects/2/editproject there is not the update form I want, the page exists but it contains nothing
You have to pass the project = get_object_or_404(Project, pk=project_id) in the dictionary in order to get the value in template.
Change your view like this:
#login_required
def editproject(request, project_id):
project = get_object_or_404(Project, pk=project_id)
if request.method == 'POST':
if request.POST['title'] and request.POST['content']:
project.title = request.POST['title']
project.content = request.POST['content']
project.developer = request.user
project.save()
return redirect('/projects/',project.project_id)
return render(request, 'projects/editproject.html', {'error':'All fields are required.','project':project})
And in the template you can get value like this :
Also in your template change method=put to method=post
and also you need to provide action if you want to update.
<div class="form-group">
<label for="exampleFormControlTextarea1">Title of the project</label>
<textarea class="form-control" id="exampleFormControlTextarea1" rows="1" name="title">
{{project.title}}
</textarea>
</div>

Displaying a django variable as the value attribute of a form field

I want text to already be in my form when I load it and I thought a good way to do this would be to add a value attribute. However it is tricky because I am using django forms which generate html after the fact. So this is the relevant part of how I display my form in my template:
<form role="form" action="/editfeedback/{{feedback.id}}/{{board}}/" method="post">
{% csrf_token %}
{% for field in form %}
{% if field.name == "title" %}
<div class="form-group">
<label class="col-sm-2 control-label" for="id_{{ field.name }}">{{ field.label }}</label>
<div class="col-sm-10">
{{ field|attr:"value:THIS IS THE TITLE" }}
</div>
</div>
{% elif field.name == "body" %}
<div class="form-group">
<label class="col-sm-2 control-label" for="id_{{ field.name }}">{{ field.label }}</label>
<div class="col-sm-10">
{{ field|attr:"value:THIS IS THE BODY" }}
</div>
</div>
{% else %}
<div class="form-group">
<label class="col-sm-2 control-label" for="id_{{ field.name }}">{{ field.label }}</label>
<div class="col-sm-10">
{{ field|attr:"value:tag" }}
</div>
</div>
{% endif %}
{% endfor %}
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary" value="Save">{% trans "Save" %}</button><br>
</div>
</div>
</form>
This works well for displaying predetermined text but I have a list of posts and each post has an edit form attached. My question is: is there a way I can set the field value attribute to a django variable? I have tried {{ field|attr:"value:{{post.title}}" }} but that gives me the following error:
TemplateSyntaxError
attr requires 2 arguments, 1 provided
EDIT: adding EditFeedbackForm from forms.py and edit_feedback view from views.py
class EditFeedbackForm(forms.Form):
title = forms.CharField(label='Title', widget=forms.TextInput(attrs={'class':'form-control'}))
body = forms.CharField(label='Body', widget=forms.Textarea(attrs={'class':'form-control'}))
tags = forms.CharField(label='Tags', widget=forms.TextInput(attrs={'class':'form-control'}))
And my view:
#login_required
def edit_feedback(request, feedback_id, board):
boardObj = Board.objects.get(board_name=board)
post = Feedback.objects.get(id=feedback_id)
form = EditFeedbackForm(initial={'title': post.feedback_title, 'body': post.feedback_description})
if request.method == 'POST':
form = EditFeedbackForm(request.POST)
if form.is_valid():
title = form.cleaned_data['title']
body = form.cleaned_data['body']
tags = form.cleaned_data['tags']
post.feedback_title = title
post.feedback_description = body
post.feedback_last_modified = datetime.now()
post.save(update_fields=['feedback_title', 'feedback_description', 'feedback_last_modified'])
# print "saved feedback " + str(post.id)
else:
form = EditFeedbackForm()
return redirect('/board/' + board + '/')
In your view, when initializing the form, you can assign initial values:
form = WhatsYourFormsClass(initial={'field1': value1, 'field2': value2})
EDIT:
You initialise the form with initial values correctly:
(...)
form = EditFeedbackForm(initial={'title': post.feedback_title, 'body': post.feedback_description})
if (...)
but then pass another (empty) instance:
(...)
else
form = EditFeedbackForm()
return (...)
Your method should be like that:
#login_required
def edit_feedback(request, feedback_id, board):
boardObj = Board.objects.get(board_name=board)
post = Feedback.objects.get(id=feedback_id)
if request.method == 'POST':
form = EditFeedbackForm(request.POST)
(form processing)
(if processing went OK, redirect to your chosen view)
else:
form = EditFeedbackForm(initial={'title': post.feedback_title, 'body': post.feedback_description})
return render(**your form template name here**, {'form': form})

django: form.is_valid always giving error

i am working on login page but my form always gives me error, please find my code below, please see view.py it response always go to else statment of "if form.is_valid():" condition. please help
here you can see form.py
from django.contrib.auth.models import User
from django import forms
class LoginForm(forms.ModelForm):
password = forms.CharField(widget=forms.PasswordInput)
class Meta:
model = User
fields = ['username', 'password']
this is my view.py
def login_view(request):
template_name = 'user/login.html'
form_class = LoginForm
if request.method == "POST":
form = form_class(data=request.POST)
if form.is_valid():
username = form.cleaned_data['username']
password = form.cleaned_data['password']
user = authenticate(username=username, password=password)
try:
login(request, user)
return redirect('user:profile')
except:
form = form_class(None)
return render(request, template_name, {'form': form, 'custom_error': 'Error'})
else:
return HttpResponse("<h1>Data is invalid</h1>")
else:
if request.user.is_authenticated():
return redirect('user:profile')
else:
form = form_class(None)
return render(request, template_name, {'form': form,})
user/login.html
{% block body %}
<div class="section">
<div class="container">
<div class="row">
<form class="form-horizontal" role="form" action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
<div class="col-md-5">
<img src="{% static 'user/images/favicon.png' %}" class="img-responsive">
</div>
<div class="col-md-6">
{% include 'user/form-template.html' %}
<div class="col-sm-offset-2 col-sm-10" align="right">
<button type="submit" class="btn btn-default" autofocus>Sign In</button>
</div>
</div>
</form>
</div>
</div>
</div>
{% endblock %}
and finally this is my form-template.html file
{% for field in form %}
<div class="col-md-8">
<div class="col-md-4">
<label>{{ field.label_tag }} </label>
</div>
<div class="col-md-4">
{{ field }}
<div>
<span>{{ field.errors }}</span>
</div>
<div>
<span class="text-danger small">{{ custom_error }}</span>
</div>
</div>
</div>
{% endfor %}
If you are using ModelForm, you are going to operate on django model objects, creating, updating, etc. If you are only trying to login, use Form instead.
Thanks its solved, I just Changed My Form no it looks like this.
class LoginForm(forms.Form):
username = forms.CharField()
password = forms.CharField(widget=forms.PasswordInput)
now i am not using built-in form and i also changed ModelForm to Form.