我正在尝试制作允许用户更改密码的表单和方法。当用户将其密码键入两个输入而不是表单时,将检查它们是否相同并更新db。但是我得到了这个错误,我无法解决它。
错误消息
<html>
<head>
<script src="../src/Brython3.2.8/brython.js"></script>
<script src="../src/Brython3.2.8/brython_stdlib.js"></script>
</head>
<body onload="brython()">
<script type="text/python">
import sys
from browser import document, alert
def echo(ev):
alert(document["zone"].value)
document['mybutton'].bind('click', echo)
</script>
<input id="zone"><button id="mybutton">click !</button>
</body>
</html>
urls.py
NoReverseMatch at /blog/password_change/blue/
Reverse for 'profile' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'blog/profile/(?P<username>[-\\w.]+)/$']
views.py
url(r'^password_change/(?P<username>[-\w.]+)/$', views.password_change, name='password_change'),
url(r'^profile/(?P<username>[-\w.]+)/$', views.profile, name='profile'),
profile.html。这是模板。
def password_change(request, username):
if request.method == 'POST':
form = PasswordChangeForm(data=request.POST, user=request.user)
if form.is_valid():
form.save()
update_session_auth_hash(request, form.user)
return redirect(reverse('blog:profile'))
else:
return redirect(reverse('blog:profile'))
else:
form = PasswordChangeForm(user=request.user)
return HttpResponseRedirect('/blog/')
views.py
<form class="form-horizontal" role="form" action="{% url'blog:password_change' user.username %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
<label for="password1">new password</label>
<div class="row">
<input type="password" name="password1" id="password1"/></div>
<div class="row">
<label for="password2">password check</label></div>
<div class="row">
<input type="password" name="password2" id="password2"/></div>
<div class="row">
<button type="submit" class="button-primary">change password</button></div>
这是forms.py
def password_change(request, username):
if request.method == 'POST':
form = PasswordChangeForm(data=request.POST, user=request.user)
if form.is_valid():
form.save()
update_session_auth_hash(request, form.user)
return redirect(reverse('blog:profile'))
else:
return redirect(reverse('blog:profile'))
else:
form = PasswordChangeForm(user=request.user)
return HttpResponseRedirect('/blog/')
答案 0 :(得分:2)
正如您的错误所述,您需要在个人资料网址中提供您的用户名参数,而您根本不提供该参数。
例如:
return redirect(reverse('blog:profile'))
应该是
return redirect(reverse('blog:profile', args=[form.user.get_username()]))
答案 1 :(得分:0)
您必须将用户名传递给reverse()。
username = form.user.get_username()
reverse('blog:profile',kwargs = {'username':username})