Hillary Sanders Hillary Sanders - 2 months ago 32
Python Question

Submitting a form fails, but because views.auth_login (not related to my form) didn't return an HttpResponse object

OK, so I have a functional auth_login setup. But it's unrelated to my

Articles
model, and
ArticleForm
ModelForm. However, when I try to create a new article on the local website, I get an error related to views.auth_login even though auth_login isn't referenced anywhere (to my knowledge) in my Article stuff:
The view home.views.auth_login didn't return an HttpResponse object. It returned None instead.
Usually, an error like that means that you're not returning an actual response in a view definition, but I do. The real question is why
home.views.auth_login
is being called instead of
home.views.add_article
. Here's my code:

home/models.py



class Article(models.Model):
headline = models.CharField(max_length=50)
content = models.CharField(max_length=1024)

def __str__(self):
return self.headline


home/forms.py



from django.contrib.auth.models import User
from .models import Article

class LoginForm(forms.ModelForm):
class Meta:
model = User
fields = ["username", "password"]


class ArticleForm(forms.ModelForm):
class Meta:
model = Article
fields = ['headline', 'content']


home/views.py



def auth_login(request):
if request.method == "POST":
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
login(request, user)
# Redirect to a success page.
return HttpResponseRedirect('/home/')
else:
# Return an 'invalid login' error message.
return HttpResponse('Invalid username / password. :( Try again? <3')

else:
loginform = LoginForm()

context = {
'loginform': loginform
}
return render(request, 'home/login.html', context)

def add_article(request):

if request.method == "POST":
form = ArticleForm(data=request.POST)
if form.is_valid():
article = form.save()
article.save()
# todo change to view article page
return HttpResponseRedirect('/home/')
else:
return HttpResponse('Invalid Inputs. :( Try again? <3')
else:
form = ArticleForm()

context = {
'form': form,
}

return render(request, 'home/add_article.html', context)


home/urls.py



...
urlpatterns = [
# match to ''
# ex: /polls/
url(r'^$', views.auth_login, name='login'),
url(r'^home/$', views.index, name='index'),
url(r'^articles/add/$', views.add_article, name='add_article')
]


home/templates/home/add_article.html



<h2> Add an Article </h2>
<form action="/" method="post">
{% csrf_token %}
{{ form }}
<br><br>
<input type="submit" value="Submit" name="addArticle" class="btn col2"/>
</form>


Results / Problem



When I go to
http://127.0.0.1:8000/articles/add
, fill out my simple form, and click submit, I get:

The view home.views.auth_login didn't return an HttpResponse object. It returned None instead.

File "/Users/hills/Desktop/code/django-beanstalk/ebenv/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner
39. response = get_response(request)

File "/Users/hills/Desktop/code/django-beanstalk/ebenv/lib/python2.7/site-packages/django/core/handlers/base.py" in _legacy_get_response
249. response = self._get_response(request)

File "/Users/hills/Desktop/code/django-beanstalk/ebenv/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
198. "returned None instead." % (callback.__module__, view_name)

Exception Type: ValueError at /
Exception Value: The view home.views.auth_login didn't return an HttpResponse object. It returned None instead.


But I can't figure out why
home.views.auth_login
is being called instead of
home.views.add_article
. I've tried deleting and recreating all db tables (
python manage.py flush
, then
python manage.py makemigrations
, then
python manage.py migrate
), and I've even tried independently writing an independent Article2 model / form / template set, but I get the same error. --> Any idea what's going on?

Answer

Well your form is submitting to the root of your website, so that's why it's hitting auth_login instead of add_article.

Change <form action="/" method="post"> to just <form method="POST">. I assume the "other" error (no HttpResponse object) is just a side effect of POSTing to auth_login.

Comments