[Django]-The view ats.views.index didn't return an HttpResponse object. It returned None instead

4๐Ÿ‘

โœ…

The request can be a POST request, but where the form.is_valid() does not hold. In that case, your view, will return None.

You should โ€œunindentโ€ the return render(..) part:

from django.shortcuts import redirect

def index(request):
    if request.method == 'POST':
        form = AuthenticationForm(data=request.POST)
        if form.is_valid():
            user = form.get_user()
            login(request, user)
            return redirect('some-view-name')
    else:
        form = AuthenticationForm()
    return render(request, 'index.html', {'form': form})

Note that here your view does not performs authentication. You should first check if the password (or some other token) matches. See the documentation on authenticate(..) [Django-doc] for more information.

In case of a successful POST request, you should make a redirect to implement the Post/Redirect/Get pattern [wiki].

3๐Ÿ‘

Rearranging the code should work

def index(request):
    form = AuthenticationForm()
    if request.method == 'POST':
        form = AuthenticationForm(data=request.POST)
        if form.is_valid():
            user = form.get_user()
            login(request, user)
            return render(request, "homepage.html", context_instance=RequestContext(request))

    return render(request, "index.html", {'form': form})

2๐Ÿ‘

You are getting this problem because you havenโ€™t written a HttpResponse object if the form is not valid.

To overcome this in your view write return an HTTP response if the form is not valid.

def index(request):
    if request.method == 'POST':
        form = AuthenticationForm(data=request.POST)
        if form.is_valid():
            user = form.get_user()
            login(request, user)
            return render(request, "homepage.html", context_instance=RequestContext(request))
        else:
            return render(request, "index.html", {'form': form, 'errors': form.errors})

    else:
        form = AuthenticationForm()
        return render(request, "index.html", {'form': form})

in index.html

{% block content %}
    {% if errors %}
        {{ errors }}
    {% endif %}
    <form class="box" method = "post">
        {% csrf_token %}
        <h1>Ats</h1>
        {{ form }}
      <input type="submit" name="" value="Login">

    </form>
{% endblock %}

or you can simply handle conditions and return default request if the request is not a post request.

def index(request):
    form = AuthenticationForm()
    if request.method == 'POST':
        form = AuthenticationForm(data=request.POST)
        if form.is_valid():
            user = form.get_user()
            login(request, user)
            return render(request, "homepage.html", context_instance=RequestContext(request))

    return render(request, "index.html", {'form': form})
๐Ÿ‘คAbu Sayem

1๐Ÿ‘

You need to define the else condition if form is not valid.

def index(request):
    if request.method == 'POST':
        form = AuthenticationForm(data=request.POST)
        if form.is_valid():
            user = form.get_user()
            login(request, user)
            return render(request, "homepage.html", context_instance=RequestContext(request))
        else:
            return render(request, "index.html", {'form': form, 'errors': form.errors})

    else:
        form = AuthenticationForm()
        return render(request, "index.html", {'form': form})

In your template you can show form errors by accessing this context variable {{ errors }}.

{% block content %}
    {% if errors %}
        {{ errors }}
    {% endif %}
    <form class="box" method = "post">
        {% csrf_token %}
        <h1>Ats</h1>
        {{ form }}
      <input type="submit" name="" value="Login">

    </form>
{% endblock %}
๐Ÿ‘คNalin Dobhal

Leave a comment