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})
- [Django]-Django admin with websocket
- [Django]-Render static html page inside my base django template
- [Django]-Geodjango distance btw points in two models
- [Django]-Get image full url on Django Channel Response
- [Django]-Listen to mqtt topics with django channels and celery
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})
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 %}
- [Django]-AttributeError: 'SigSafeLogger' object has no attribute 'logger'
- [Django]-Django manage.py raising ImproperlyConfigured error
- [Django]-Django โ how to write users and profiles handling in best way?
- [Django]-Bulk insert on multi-column unique constraint Django