我收到错误,如下所示。
Traceback (most recent call last):
File "C:\Users\Tyler Durden\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\exception.py", line 35, in inner
response = get_response(request)
File "C:\Users\Tyler Durden\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py", line 128, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\Tyler Durden\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Tyler Durden\Desktop\ProductHunt\accounts\views.py", line 10, in signup
if request.POST['password1'] == request.POST['password2']:
File "C:\Users\Tyler Durden\AppData\Local\Programs\Python\Python37\lib\site-packages\django\utils\datastructures.py", line 79, in __getitem__
raise MultiValueDictKeyError(key)
django.utils.datastructures.MultiValueDictKeyError: 'password1'
[24/Apr/2019 04:39:23] "POST /accounts/logout HTTP/1.1" 500 74823
signup.html
<form method="POST" action="{% url 'signup' %}">
{% csrf_token %}
Username:
<br />
<input type="text" name="username" />
<br />
Password:
<br />
<input type="password" name="password1" />
<br />
Confirm Password:
<br />
<input type="password" name="password2" />
<br />
<br />
<input class="btn btn-primary" type"submit" value="Sign Up!">
base.html文件
<ul class="navbar-nav ml-auto">
{% if user.is_authenticated %}
<li class="nav-item">
<a class="nav-link" href="javascript:{document.getElementById('logout').submit()}">Logout</a>
</li>
<form id ="logout" method="POST" action="{% url 'logout' %}">
{% csrf_token %}
<input type="hidden" />
</form>
{% else %}
<li class="nav-item">
<a class="nav-link" href="{% url 'signup' %}">Signup</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'login' %}">Login</a>
</li>
{% endif %}
</ul>
views.朋友
def signup(request):
if request.method== 'POST':
#User to sign a up for an account
if request.POST['password1'] == request.POST['password2']:
try:
user = User.objects.get(username=request.POST['username'])
return render(request,
'accounts/signup.html',
{'error': 'Username has already been taken'})
except User.DoesNotExist:
user = User.objects.create_user(request.POST['username'],
password = request.POST['password1'],
email= request.POST['emailaddress'])
auth.login(request, user)
return redirect('home')
else:
return render(request,
'accounts/signup.html',
{'error': 'Password entries do not match'})
return render(request, 'accounts/signup.html')
def login(request):
if request.method== 'POST':
user = auth.authenticate(username=request.POST['username'],
password=request.POST['password'])
if user is not None:
auth.login(request, user)
return redirect('home')
else:
return render(request,
'accounts/login.html',
{'error': 'Username or Password is incorrect'})
else:
return render(request, 'accounts/login.html')
def logout(request):
if request.method == 'POST':
auth.logout(request)
return redirect('home')
#TODO Need to route to homepage and Log them out
return render(request, 'accounts/signup.html')
看起来你不小心将你的logout
路径发送到login
的urls.py
视图。确保你的网址看起来像这样:
urlpatterns = [
...
path('accounts/logout/', views.logout, name='logout'),
...
]
改为这些代码,我为django bootcamp课程编写了这些代码并为我工作:
signup.html
<form method="POST" action="{% url 'signup' %}"">
{% csrf_token %}
User name :
<br />
<input type="text" name="username" />
<br />
Password :
<br />
<input type="password" name="password1" />
<br />
Confirm Password :
<br />
<input type="password" name="password2" />
<br /><br />
<input class="btn btn=primary" type="submit" value="sign up !" />
</form>
views.朋友
def signup(request):
if request.method=="POST":
if request.POST['password1']==request.POST['password2']:
try:
user = User.objects.get(username=request.POST['username'])
return render(request, 'accounts/signup.html', {'error':'username has already been taken'})
except User.DoesNotExist:
user = User.objects.create(username=request.POST['username'], password=request.POST['password1'])
auth.login(request,user)
return redirect('home')
else:
return render(request, 'accounts/signup.html',{'error':'Password Not Match'})
return render(request, 'accounts/signup.html')
def login(request):
if request.method == 'POST':
user = auth.authenticate(username=request.POST['username'],password=request.POST['password'])
if user is not None:
auth.login(request, user)
return redirect('home')
else:
return render(request, 'accounts/login.html' ,{'error':'username or password is incorrect.'})
return render(request, 'accounts/login.html')
def logout(request):
if request.method == 'POST':
auth.logout(request)
return redirect('home')
URLs.朋友
from django.urls import path
from . import views
urlpatterns = [
path('signup/', views.signup, name='signup'),
path('login/', views.login, name='login'),
path('logout/', views.logout, name='logout'),
]
base.html文件
<header>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container">
<a class="navbar-brand" href="{% url 'home' %}">
<img src="{% static 'products/logo.png' %}" height=30 width=30 class="d-inline-block alighn-top" />
Product Hunt
</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNavAltMarkup">
<div class="navbar-nav ml-auto">
{% if user.is_authenticated %}
<a class="nav-item nav-link" href="javascript:{document.getElementById('logout').submit()}" onclick="">Logout</a>
<form id="logout" method="POST" action="{% url 'logout' %}">
{% csrf_token %}
<input type="hidden" />
</form>
{% else %}
<a class="nav-item nav-link" href="{% url 'signup' %}">Sign Up</a>
<a class="nav-item nav-link" href="{% url 'login' %}">Login</a>
{% endif %}
</div>
</div>
</div>
</nav>
</header>