reg: Django forms and models

29 views
Skip to first unread message

Amitesh Sahay

unread,
Nov 11, 2019, 7:37:24 PM11/11/19
to Django Users
Hello Members,

I am creating a simple django forms and integrating it with models. I have followed the django docs to perform the task, but I believe that I am doing some mistake due to which I am getting
"ValueError at /blog/contact/". Below is the full error stack:

Environment:


Request Method: GET
Request URL: http://localhost:8000/blog/contact/

Django Version: 2.2.5
Python Version: 3.6.8
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'BLOG']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "/root/PycharmProjects/myvenv/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
  34.             response = get_response(request)

File "/root/PycharmProjects/myvenv/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  126.                 "returned None instead." % (callback.__module__, view_name)

Exception Type: ValueError at /blog/contact/
Exception Value: The view BLOG.views.contact didn't return an HttpResponse object. It returned None instead.
Below are my python files snippet:

models.py
-------------
from django.db import models


class Contact(models.Model):
name = models.CharField(max_length=100)
email = models.EmailField(max_length=200)
phone = models.IntegerField()
Comment = models.TextField(blank=False)

def __str__(self):
return self.email
forms.py
from django import forms
from .models import Contact


class ContactForm(forms.ModelForm):
name = forms.CharField(label='Your name', max_length=100)
email = forms.EmailField(label='Your Email Address', max_length=200)
phone = forms.IntegerField(label='Your Phone Number', max_value=20)
Comment = forms.TextInput()

class Meta:
model = Contact
fields = ('name', 'email', 'phone', 'Comment')
views.py
from django.http import HttpResponse
from django.shortcuts import render, redirect

from .forms import ContactForm


def contact(request):
if request.method == 'POST':
name_r = request.POST.get('name')
email_r = request.POST.get('email')
phone_r = request.POST.get('phone')
comment_r = request.POST.get('comment')

form = ContactForm(name=name_r, email=email_r, phone=phone_r, comment=comment_r)
if form.is_valid():
form.save()
form = ContactForm()
return HttpResponse('Thank you for your enquiry')
return redirect(request, 'contact.html')
else:
form = ContactForm()
return render(request, 'contact.html', {'form': form})
Any suggestions....


Regards,
AS

Kasper Laudrup

unread,
Nov 11, 2019, 8:32:22 PM11/11/19
to django...@googlegroups.com
Hi Amitesh,

In this view:

On 11/11/2019 20.36, 'Amitesh Sahay' via Django users wrote:
>
> def contact(request):
> if request.method =='POST':
> name_r = request.POST.get('name')
> email_r = request.POST.get('email')
> phone_r = request.POST.get('phone')
> comment_r = request.POST.get('comment')
>
> form = ContactForm(name=name_r, email=email_r, phone=phone_r, comment=comment_r)
> if form.is_valid():
> form.save()
> form = ContactForm()
> return HttpResponse('Thank you for your enquiry')
> return redirect(request, 'contact.html')
> else:
> form = ContactForm()
> return render(request, 'contact.html', {'form': form})
>

Concider what will happen if request.method is *not* POST?

As a hint, if a Python function doesn't return anything explicitly, it
will implicitly return None.

Hope that will guide you in the right direction.

I could just solve it for you, but it's better that you figure it out
yourself if you want to learn.

Kind regards,

Kasper Laudrup

Amitesh Sahay

unread,
Nov 12, 2019, 6:39:47 PM11/12/19
to django...@googlegroups.com
Hello Kasper,

Thank you for the hint. I found my mistake and resolved it.

Regards,
Amitesh




--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsub...@googlegroups.com.

To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/43c7a649-b008-d0ef-dd2c-64679c270017%40stacktrace.dk.

o1bigtenor

unread,
Nov 13, 2019, 12:13:22 AM11/13/19
to django...@googlegroups.com
On Tue, Nov 12, 2019 at 12:39 PM 'Amitesh Sahay' via Django users
<django...@googlegroups.com> wrote:
>
> Hello Kasper,
>
> Thank you for the hint. I found my mistake and resolved it.
>
So what did you do?
(doesn't need to have all the detail just the idea please)

TIA

Amitesh Sahay

unread,
Nov 13, 2019, 7:06:44 PM11/13/19
to django...@googlegroups.com
Initially I had written the "else" statement at the wrong place. Moved it under the very first "if" and used then returned the HTTPresponse on the right place inside the function

Regards,
Amitesh S




--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages