Issue in form validation - python

I want to validate my update form using form validation. When i leave a field blank and hit update button I get a blank page showing the error message. I want that error message to be displayed on top of that particular field when I leave it blank and hit update button.
This is my latest update view:
def update(request, id):
item = get_object_or_404(BookEntry, id=id)
if request.method == "POST":
form = UpdateForm(request.POST, instance=item)
# print template
error_messages = {
'error': form.errors,
}
if form.is_valid():
print(form.cleaned_data)
post = form.save(commit=False)
post.save()
return HttpResponseRedirect(reverse('bms:index'), id)
else:
form = UpdateForm()
return HttpResponse(json.dumps(error_messages))
return render(request, 'index.html',{'form':form})
This is my old update view:
def update(request, id):
item = get_object_or_404(BookEntry, id=id)
if request.method=="POST":
form = UpdateForm(request.POST, instance=item)
if form.is_valid():
post=form.save(commit=False)
post.save()
return HttpResponseRedirect(reverse('bms:index'), id)
else:
form=UpdateForm(instance=item)
return HttpResponseRedirect(reverse('bms:index'), id)
return render(request, 'bms.html',{'form':form})
This is my forms.py:
class UpdateForm(forms.ModelForm):
title = forms.CharField(max_length=100)
author = forms.CharField(max_length=100)
edition = forms.CharField(max_length=100)
publisher = forms.CharField(max_length=100)
genre = forms.CharField(max_length=100)
detail = forms.CharField(max_length=100)
language = forms.CharField(max_length=100)
price = forms.IntegerField()
dop = forms.CharField(max_length=100)
cop = forms.CharField(max_length=100)
copyright = forms.CharField(max_length=100)
isbn = forms.IntegerField()
class Meta:
model = BookEntry
fields = '__all__'
This is my html form:
<form action="{% url 'bms:update' book.id %}" id="updateform" name="updateform" method="POST">
{% csrf_token%}
<div class = "form-group">
<label for = "title">
Title:
</label>
<input class = "form-control" id="book_title" type = "text" name="title" value="{{ book.title }}">
</div>
<div class="form-group">
<label for = "author">
Author:
</label>
<input id="book_author" class = 'form-control' type = "text" name="author" value="{{ book.author }}">
</div>
<div class = "form-group">
<label for = "edition">
Edition:
</label>
<input id="book_edition" type = "text" class = 'form-control' name="edition" value="{{ book.edition }}">
</div>
<div class = "form-group">
<label for ="publisher">
Publisher:
</label>
<input id="book_publisher" type = "text" name="publisher" class = 'form-control' value="{{ book.publisher }}"/>
</div>
<div class = "form-group">
<label for ="genre">
Genre:
</label>
<input id="book_genre" type = "text" name="genre" class = 'form-control' value="{{ book.genre }}"/>
</div>
<div class = "form-group">
<label for ="detail">
Detail:
</label>
<input id="book_detail" type = "text" name="detail" class = 'form-control' value="{{ book.detail }}"/>
</div>
<div class = "form-group">
<label for ="language">
Language:
</label>
<input id="book_language" type = "text" name="language" class = 'form-control' value="{{ book.language }}"/>
</div>
<div class = "form-group">
<label for ="price">
Price:
</label>
<input id="book_price" type = "text" name="price" class = 'form-control' value="{{ book.price }}"/>
</div>
<div class = "form-group">
<label for ="dop">
DOP:
</label>
<input id="book_dop" type = "text" name="dop" class = 'form-control' value="{{ book.dop }}"/>
</div>
<div class = "form-group">
<label for ="cop">
COP:
</label>
<input id="book_cop" type = "text" name="cop" class = 'form-control' value="{{ book.cop }}"/>
</div>
<div class = "form-group">
<label for ="cop">
Copyright
</label>
<input id="book_copyright" type = "text" name="copyright" class = 'form-control' value="{{ book.copyright }}"/>
</div>
<div class = "form-group">
<label for ="isbn">
ISBN:
</label>
<input id="book_isbn" type = "text" name="isbn" class = 'form-control' value="{{ book.isbn }}"/>
</div>
<input type = "submit" value="Update" id="update" class = "btn btn-success" style="font-size:18px;" />
</form>
This is the URL it shows when a field is left empty:
http://127.0.0.1:8000/bms/update/2/
And this is the error message:
{"error": {"edition": ["This field is required."]}}
I had left edition field empty to check if validation is working properly or not.
Can anyone suggest me how to do it the right way?

As Daniel pointed out, you need to re-render the form.
The basics of HTTP Form Post are:
If the form is valid: save + do a HttpResponseRedirect
If the form is not valid; re-render the same template with the errors
Change your function to:
def update(request, id):
item = get_object_or_404(BookEntry, id=id)
if request.method == "POST":
form = UpdateForm(request.POST, instance=item)
# print template
error_messages = {
'error': form.errors,
}
if form.is_valid():
print(form.cleaned_data)
post = form.save(commit=False)
post.save()
return HttpResponseRedirect(reverse('bms:index'), id)
return render(request, 'index.html',{'form':form}) # Form invalid case is handled here; the form is reused from above; form.errors is available in the template now
else:
pass # Handle GET requests here

Related

Django HttpResponseRedirect doesn´t lead to the page

I am working on the CS50 project2 commerce. I try to create a new list but when I click on the submit button, it doesn´t redirect to the index page as I want. Anybody can help please? I get the error message: auctions.models.Category.DoesNotExist: Category matching query does not exist. However I can add list directly from admin page.
views.py
def createlisting(request):
if request.method == "GET":
allCategories = Category.objects.all()
return render(request, "auctions/create.html", {
"categories": allCategories
})
else:
# get the data from the form
title = request.POST["title"]
description = request.POST["description"]
imageurl = request.POST["imageurl"]
price = request.POST["price"]
category = request.POST["category"]
currentuser = request.user
# get the contents
categoryData = Category.objects.get(categoryName=category)
#create new list object
newListing = Listing(
title=title,
description=description,
imageUrl=imageurl,
price=float(price),
category=categoryData,
owner=currentuser
)
newListing.save()
return HttpResponseRedirect(reverse("index"))
create.html
{% extends "auctions/layout.html" %}
{% block body %}
<h2>Create New Listing</h2>
<form action = "{% url 'create' %}" method="POST">
{% csrf_token %}
<div class="form-group">
<label for="title">Title</label>
<input type="text" name="title" class="form-control" id="title" placeholder="Enter title">
</div>
<div class="form-group">
<label for="description">Description</label>
<input type="text" name="description" class="form-control" id="description" placeholder="Enter Description">
</div>
<div class="form-group">
<label for="imageurl">Image URL</label>
<input type="text" name="imageurl" class="form-control" id="imageurl" placeholder="Image Url">
</div>
<div class="form-group">
<label for="price">Price</label>
<input type="number" name="price" class="form-control" id="price" placeholder="Price">
</div>
<div class="form-group">
<label for="category">Choose a Category</label>
<select name="category" id="category">
{% for category in categories %}
<option value="category">{{category}}</option>
{% endfor %}
</select>
</div>
<button type="submit" class="btn btn-success">Create New Listing</button>
</form>
{% endblock %}
Use redirect. And you don't need to use else after returning in the if statement.
from django.shortcuts import redirect
def createlisting(request):
if request.method == "GET":
allCategories = Category.objects.all()
return render(request, "auctions/create.html", {
"categories": allCategories
})
# POST
title = request.POST["title"]
description = request.POST["description"]
imageurl = request.POST["imageurl"]
price = request.POST["price"]
category = request.POST["category"]
currentuser = request.user
# get the contents
categoryData = Category.objects.get(categoryName=category)
#create new list object
newListing = Listing(
title=title,
description=description,
imageUrl=imageurl,
price=float(price),
category=categoryData,
owner=currentuser
)
newListing.save()
return redirect("index")
Try this view:
def createlisting(request):
allCategories=""
if request.method == "GET":
allCategories = Category.objects.all()
else:
# get the data from the form
title = request.POST["title"]
description = request.POST["description"]
imageurl = request.POST["imageurl"]
price = request.POST["price"]
category = request.POST["category"]
currentuser = request.user
# get the contents
categoryData = Category.objects.get(categoryName=category)
#create new list object
newListing = Listing(
title=title,
description=description,
imageUrl=imageurl,
price=float(price),
category=categoryData,
owner=currentuser
)
newListing.save()
return redirect("index")
return render(request, "auctions/create.html", {
"categories": allCategories
})

Trying to pass url in form action

I'm new in django and i'm stuck now.
I'm trying to pass the url in form [action] attribute that would go to my edit function defined in [views.py] file and do it's job but whenever I try to pass the url [NoReverseMatch] is shown.
This is what i tried to do:
<div class="modal fade" id="editform" role="dialog">
<div class="modal-dialog">
<div class = "modal-content">
<div class = "modal-header">
<button type = "button" class = "close" data-dismiss="modal">×</button>
<h3 class="modal-title">
<b>Edit Information</b>
</h3>
</div>
<div class = "modal-body">
<form action="{% url 'studentapp:editrow' rowid=id %}" id="editform" method="POST">
{% csrf_token %}
<div class = "form-group">
<label for = "your_name">
Your name:
</label>
<input class = "form-control" id="new_name" type = "text" name="name" value="{{ student_detail.name }}" placeholder="Enter your name">
</div>
<div class="form-group">
<label for = "course_name">
Course:
</label>
<input id="new_course" class = 'form-control' type = "text" name="course" value="{{ student_detail.course }}" placeholder="Enter your course">
</div>
<div class = "form-group">
<label for = "rollno">
Roll No.:
</label>
<input id="new_rollno" type = "text" class = 'form-control' name="roll" value="{{ student_detail.roll }}" placeholder="Enter your roll number">
</div>
<div class = "form-group">
<label for ="addr">
Address:
</label>
<input id="new_address" type = "text" name="address" class = 'form-control' value="{{ student_detail.address }}" placeholder="Enter your address"/>
</div>
<input type = "submit" value="Update" id="update" class = "btn btn-success" style="font-size:18px;" />
</form>
</div>
</div>
</div>
</div>
In my urls.py I've used the following url:
url(r'^editrow/(?P<rowid>[0-9]+)/$', views.editrow, name='editrow'),
My [editrow] view looks something like this:
def editrow(request, rowid):
item = get_object_or_404(Studentapp, rowid=id)
print item
if request.method=="POST":
form = EntryForm(request.POST, instance=item)
if form.is_valid():
post=form.save(commit=False)
post.save()
return HttpResponseRedirect(reverse('studentapp:index'),rowid.id)
else:
form=EntryForm(instance=item)
return render(request, 'index.html',{'form':form})
else:
form=EntryForm(instance=item)
return render(request, 'index.html',{'form':form})
View that render's the template:
def index(request):
context = {}
latest_student = Studentapp.objects.order_by('pub_date')
context.update({
'latest_student': latest_student
})
response = {"status": False, "errors": []}
if request.is_ajax():
id = request.POST['id']
response = {}
response['status'] = False
student_detail = Studentapp.objects.filter(id=id).first()
context = {
"student_detail": student_detail
}
template = render_to_string("studentapp/_edit_student.html", context)
response['template'] = template
response['status'] = True
return HttpResponse(json.dumps(response), content_type="applicaton/json")
return render(request, "studentapp/index.html", context)
What i'm doing in crud is:
1) make an [edit] button in table through for loop.
2) when i click [edit] button a pre-populated form shows up(which i'm getting).
3) After i click the pre-populated form i want to edit that form and save it and updated data is reflected in my django db.
Thanks to #Alasdair, I looked in my code what he was trying to tell me and i got the answer.
The url that i was trying to pass through my action attribute was wrong. Here's what i did.
<form action="{% url 'studentapp:editrow' rowid=student_detail.id %}" id="editform" method="POST">
Through "student_detail" i'm able to get pre-populated form as i mentioned above. i used the same to get the id and pass it to my "editrow" view.
It's working for me now.
It seems like you never add id to your context in your view index. So the template does not have that variable available.
You need to add that id to your context.
Just like #Alasdair pointed out in the comments.

Trying to edit a form using django and python

I'm trying to edit the form via my edit button.
When i click update button in my form I'm getting the following error:
FieldError at /studentapp/editrow/72/
Cannot resolve keyword 'rowid' into field Choices are: address, course, id, name, pub_date, roll
My traceback shows the error originating in this line
item = get_object_or_404(Studentapp, rowid=id) this line.
My models.py looks like this:
class Studentapp(models.Model):
name = models.CharField(max_length=100)
roll = models.IntegerField()
course = models.CharField(max_length=100)
address = models.CharField(max_length=100)
pub_date = models.DateTimeField('date published', auto_now=True)
def __str__(self):
return '%s %s %s %s' % (self.name, self.roll, self.course, self.address)
def published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
EDIT
My view:
def editrow(request, id):
item = get_object_or_404(Studentapp, id=id)
if request.method=="POST":
form = EntryForm(request.POST, instance=item)
if form.is_valid():
post=form.save(commit=False)
post.save()
return HttpResponseRedirect(reverse('studentapp:index'), id)
else:
form=EntryForm(instance=item)
return render(request, 'index.html',{'form':form})
else:
form=EntryForm(instance=item)
return render(request, 'index.html',{'form':form})
My urls.py
url(r'^editrow/(?P<rowid>[0-9]+)/$', views.editrow, name='editrow'),
Form that i'm using to update the entry:
{% load staticfiles %}
<form action="{% url 'studentapp:editrow' student_detail.id %}" id="editform" method="POST">
{% csrf_token%}
<div class = "form-group">
<label for = "your_name">
Your name:
</label>
<input class = "form-control" id="new_name" type = "text" name="name" value="{{ student_detail.name }}" placeholder="Enter your name">
</div>
<div class="form-group">
<label for = "course_name">
Course:
</label>
<input id="new_course" class = 'form-control' type = "text" name="course" value="{{ student_detail.course }}" placeholder="Enter your course">
</div>
<div class = "form-group">
<label for = "rollno">
Roll No.:
</label>
<input id="new_rollno" type = "text" class = 'form-control' name="roll" value="{{ student_detail.roll }}" placeholder="Enter your roll number">
</div>
<div class = "form-group">
<label for ="addr">
Address:
</label>
<input id="new_address" type = "text" name="address" class = 'form-control' value="{{ student_detail.address }}" placeholder="Enter your address"/>
</div>
<input type = "submit" value="Update" id="update" class = "btn btn-success" style="font-size:18px;" />
</form>
This line is not right, your model does not have the field rowid
item = get_object_or_404(Studentapp, rowid=id) this line. # WRONG
item = get_object_or_404(Studentapp, id=id) this line. # OK
and your urls should be
url(r'^editrow/(?P<id>[0-9]+)/$', views.editrow, name='editrow'),
# url(r'^editrow/(?P<rowid>[0-9]+)/$', views.editrow, name='editrow'), # WRONG

I have a bound ModelForm that is invalid

I am trying to create a ModelForm that links to an external database, and when you submit the form the external database gets updated. The problem comes when I check the validity of the form, it is invalid.
I have done some researching into this and found the most common problem was that the form is not bound, but when I use print(form.non_field_errors) I get:
<bound method BaseForm.non_field_errors of <EmailForm bound=True, valid=False, fields=(subject;body;name;altsubject;utm_source;utm_content;utm_campaign)>
models.py:
class MarketingEmails(models.Model):
messageid = models.AutoField(db_column='column1', primary_key=True)
subject = models.CharField(db_column='column2', max_length=2000)
body = models.TextField(db_column='column3') #using a text field as there is no maximum length
name = models.CharField(db_column='column4', max_length=25)
altsubject = models.CharField(db_column='column5', max_length=2000)
utm_source = models.CharField(db_column='column6', max_length=25)
utm_content = models.CharField(db_column='column7', max_length=25)
utm_campaign = models.CharField(db_column='column8', max_length=25)
class Meta:
managed = False
db_table = ''
forms.py:
class EmailForm(forms.ModelForm):
class Meta:
model = MarketingEmails
fields = ['messageid','subject','body','name','altsubject','utm_source','utm_content','utm_campaign']
views.py:
def emailinfo(request, pk):
if request.session.has_key('shortname'):
shortname = request.session['shortname']
rows = get_object_or_404(MarketingEmails, pk=pk)
if request.method == 'POST':
form = EmailForm(request.POST)
print(form.errors)
print(form.non_field_errors)
if form.is_valid():
form.save()
print("form is valid")
return redirect('marketingemails:emailinfo', pk = rows.messageid)
return render(request, 'marketingemails/emailinfo.html',{'shortname': shortname, 'rows': rows})
else:
return HttpResponseRedirect(reverse('common:login'))
urls.py:
app_name = 'marketingemails'
urlpatterns = [
url(r'^marketing/emails/(?P<pk>[0-9]+)/$', marketingviews.emailinfo, name='emailinfo'),
]
html:
<form method="POST" class="post-form" action ="">
{% csrf_token %}
<label for="exampleTextarea">Name</label>
<textarea class="form-control" id="exampleTextarea" rows="1">{{ rows.name }}</textarea>
<label for="exampleTextarea">Subject</label>
<textarea class="form-control" id="exampleTextarea" rows="1">{{ rows.subject }}</textarea>
<label for="exampleTextarea">Alternative Subject</label>
<textarea class="form-control" id="exampleTextarea" rows="1">{{ rows.altsubject }}</textarea>
<label for="exampleTextarea">Body</label>
<div class="ibox-content no-padding">
<div class="summernote">
{{ rows.body }}
</div>
</div>
<label for="exampleTextarea">utm_source</label>
<textarea class="form-control" id="exampleTextarea" rows="1">{{ rows.utm_source }}</textarea>
<label for="exampleTextarea">utm_content</label>
<textarea class="form-control" id="exampleTextarea" rows="1">{{ rows.utm_content }}</textarea>
<label for="exampleTextarea">utm_campaign</label>
<textarea class="form-control" id="exampleTextarea" rows="1">{{ rows.utm_campaign }}</textarea>
<button type="submit" class="save btn btn-default">Save</button>
</form>
Your HTML form doesn't name the fields so the form can't get them. You want to use the form for rendering too : https://docs.djangoproject.com/en/1.11/topics/forms/#working-with-form-templates

django - How to change existing code to ModelForm instance

New to Django and this is my first web application.
I'm having trouble with django's ModelForm feature and I wanted to know:
How do I modify my code so that I can create an instance of ModelForm, and specifically, how can I extract the form data to upload to the backend? I will need to reference this instance at a later time to re-populate the same data in an update_profile view but the updation can only happen once the user is logged in (after signup and profile creation).
For the editing section, do I use pk=some_record.pk? Very confused, any help is appreciated.
The model I'm working with CustomerDetail has a foreign key field customer which references the Customer model:
class CustomerDetail(models.Model):
phone_regex = RegexValidator(regex = r'^\d{10}$', message = "Invalid format! E.g. 4088385778")
date_regex = RegexValidator(regex = r'^(\d{2})[/.-](\d{2})[/.-](\d{2})$', message = "Invalid format! E.g. 05/16/91")
customer = models.OneToOneField(Customer,
on_delete=models.CASCADE,
primary_key=True,)
address = models.CharField(max_length=100)
date_of_birth = models.CharField(validators = [date_regex], max_length = 10, blank = True)
company = models.CharField(max_length=30)
home_phone = models.CharField(validators = [phone_regex], max_length = 10, blank = True)
work_phone = models.CharField(validators = [phone_regex], max_length = 10, blank = True)
def __str__(self):
return str(self.customer)
Here is a snippet of views.py:
def create_profile(request):
if request.POST:
address = request.POST['address']
date_of_birth = request.POST['date_of_birth']
company = request.POST['company']
home_phone = request.POST['home_phone']
work_phone = request.POST['work_phone']
custprofdata = CustomerDetail(address = address, date_of_birth = date_of_birth, company = company, home_phone = home_phone, work_phone = work_phone)
custprofdata.save()
output = {'address': address, 'dateofbirth': date_of_birth, 'company': company, 'homephone': home_phone, 'workphone': work_phone}
return render(request, 'newuser/profile_created.html', output)
else:
return redirect(create_profile)
And here is a snippet of the form part of the respective create_profile.html:
<form action = "{% url 'create_profile' %}" class="create_profile" role="form" method = "post">
{% csrf_token %}
<div class="form-group">
<label for="address" class="col-md-3 control-label">Address</label>
<div class="col-md-9">
<input type="text" class="form-control" name="address" placeholder="777 Park St" />
</div>
</div>
<div class="form-group">
<label for="date-of-birth" class="col-md-3 control-label">Date Of Birth</label>
<div class="col-md-9">
<input type="text" class="form-control" name="date_of_birth" placeholder="09/12/82" />
</div>
</div>
<div class="form-group">
<label for="company" class="col-md-3 control-label">Company</label>
<div class="col-md-9">
<input type="text" class="form-control" name="company" placeholder="Oracle">
</div>
</div>
<div class="form-group">
<label for="home-phone" class="col-md-3 control-label">Home Phone</label>
<div class="col-md-9">
<input type="text" class="form-control" name="home_phone" placeholder="4082992788">
</div>
</div>
<div class="form-group">
<label for="work-phone" class="col-md-3 control-label">Work Phone</label>
<div class="col-md-9">
<input type="text" class="form-control" name="work_phone" placeholder="6690039955">
</div>
</div>
<div class="form-group">
<div class="col-md-offset-3 col-md-9">
<button type = "create" class="btn btn-success" form = "create_profile"> Submit </button>
</div>
</div>
</form>
Implementing a basic ModelForm is just a matter of the following:
from django.forms import ModelForm
from .models import CustomerDetail
class CustomerDetailForm(ModelForm):
class Meta:
model = CustomerDetail
fields = ['address', 'date_of_birth', 'company', 'home_phone', 'work_phone',]
https://docs.djangoproject.com/en/1.10/topics/forms/modelforms/#a-full-example
But I suggest you also switch to using a Class Based View (CBV) - the CreateView will do the same as your existing view with much less code, with an implicit ModelForm (which you can customise by providing your own ModelForm class with form_class = YourFormClass if you want).
https://docs.djangoproject.com/en/1.10/ref/class-based-views/generic-editing/#createview
https://ccbv.co.uk/projects/Django/1.10/django.views.generic.edit/CreateView/
After creating CustomerDetailForm as #John Carter said, you might want to change your view.py to the following
def create_profile(request):
if request.POST:
form = CustomerDetailForm(request.POST)
if form.is_valid():
## save data in database ##
return render(request, 'newuser/profile_created.html', {form:form})
else:
return redirect(create_profile)

Categories