Submitting a form fails, but because views.auth_login (not related to my form) didn't return an HttpResponse object
<p>OK, so I have a functional auth_login setup. But it's unrelated to my <code>Articles</code> model, and <code>ArticleForm</code> 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: <code>The view home.views.auth_login didn't return an HttpResponse object. It returned None instead.</code> 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 <code>home.views.auth_login</code> is being called instead of <code>home.views.add_article</code>. Here's my code:</p>

<h2>home/models.py</h2>

<pre><code>class Article(models.Model):
    headline = models.CharField(max_length=50)
    content = models.CharField(max_length=1024)

    def __str__(self):
        return self.headline
</code></pre>

<h2>home/forms.py</h2>

<pre><code>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']
</code></pre>

<h2>home/views.py</h2>

<pre><code>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? &lt;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? &lt;3')
    else:
        form = ArticleForm()

        context = {
            'form': form,
        }

        return render(request, 'home/add_article.html', context)
</code></pre>

<h2>home/urls.py</h2>

<pre><code>...
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')
]
</code></pre>

<h2>home/templates/home/add_article.html</h2>

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

<h2>Results / Problem</h2>

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

<pre><code>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.
</code></pre>

<p>But I can't figure out why <code>home.views.auth_login</code> is being called instead of <code>home.views.add_article</code>. I've tried deleting and recreating all db tables (<code>python manage.py flush</code>, then <code>python manage.py makemigrations</code>, then <code>python manage.py migrate</code>), 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?</p>

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

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

python django