我的任务是仅在登录/注册操作后才能获取主页视图。例如,如果我们访问example.com,我们应该获得登录页面。如果我们之前没有注册,那么按下“登录”按钮(在输入任何用户名和密码后,因为我还没有完成注册按钮)必须在注册页面移动我们。
这里的代码是:
views.py:
from django.shortcuts import render, redirect
from django.core.urlresolvers import reverse
from django.contrib.auth import authenticate, login
from django.contrib.auth.forms import AuthenticationForm, UserCreationForm
def my_login(request):
if request.method == 'POST':
form = AuthenticationForm(request.POST)
if form.is_valid():
username = form.cleaned_data['username']
password = form.cleaned_data['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
return render(request, 'home.html', {})
else:
# Return a 'disabled account' error message
pass
else:
# send to signup
return render(request, 'signup.html', {})
else:
form = AuthenticationForm()
return render(request, 'login.html', {'form': form})
这是我的login.html:
{% load staticfiles %}
{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}
{% if next %}
{% if user.is_authenticated %}
<p>Your account doesn't have access to this page. To proceed,
please login with an account that has access.</p>
{% else %}
<p>Please login to see this page.</p>
{% endif %}
{% endif %}
<form method="post" action="{% url 'my_login' %}">
{% csrf_token %}
<table>
<tr>
<td>{{ form.username.label_tag }}</td>
<td>{{ form.username }}</td>
</tr>
<tr>
<td>{{ form.password.label_tag }}</td>
<td>{{ form.password }}</td>
</tr>
</table>
<input type="submit" value="Login" />
<input type="hidden" name="next" value="{{ next }}" />
</form>
目前显示错误(点击“登录”按钮后) -
/ login /的ValueError 视图evr.views.my_login未返回HttpResponse对象。它改为返回None。
但重点不仅仅是这个错误。我一般都不明白我的方向是否合适?
答案 0 :(得分:1)
您的登录网址有两条可能的路径,目前无法返回响应,因此您需要在以下时间确定需要显示的内容:
所有视图必须返回响应对象