这是我的整个urls.py文件,我设计了自定义模板以重置密码功能
from django.conf.urls import include, url
from django.contrib import admin
from django.conf import settings
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^articles/', include('article.urls')),
url(r'^', include('django.contrib.auth.urls')),
url(r'^password_reset/$', 'django.contrib.auth.views.password_reset',{'template_name': 'reset_password.html'}),
url(r'^password_reset/done/$', 'django.contrib.auth.views.password_reset_done',{'template_name': 'password_reset_done.html'}),
url(r'^reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$', 'django.contrib.auth.views.password_reset',{'template_name': 'password_confirm.html'}),
url(r'^reset/done/$', 'django.contrib.auth.views.password_reset_complete',{'template_name': 'password_reset_complete.html'}),
#
url(r'^', include('article.urls')),
# user auth urls
url(r'^home/$', 'defcon.views.home'),
url(r'^accounts/login/$', 'defcon.views.login'),
url(r'^accounts/auth/$', 'defcon.views.auth_view'),
url(r'^accounts/logout/$', 'defcon.views.logout'),
url(r'^accounts/loggedin/$', 'defcon.views.loggedin'),
url(r'^accounts/invalid/$', 'defcon.views.invalid_login'),
# user registration urls
url(r'^accounts/register/$', 'defcon.views.register_user'),
url(r'^accounts/register_success/$', 'defcon.views.register_success'),
url(r'^accounts/register_auth/$', 'defcon.views.register_auth'),
#Serve Media Files
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT,
}),
]
我查看了django文档@ https://docs.djangoproject.com/en/1.8/topics/auth/default/,并从那里尝试添加这样的自定义模板
url(
'^password_reset/',
'django.contrib.auth.views.password_reset',
{'template_name': 'change-password.html'}
)
我正在使用django UI,但我有自定义模板,但没有显示
{% extends "user_forms.html" %}
{% block main %}
<span class="fa fa-user bigicon"></span>
<h2>Enter Details</h2>
<form action = "" method = "POST">{% csrf_token %}
<small>{{form.email.errors}}</small>
<p>{{form.email}}</p>
<button class="btn btn-default" type="submit" name="submit" />Reset my password</button>
</form>
<script type="text/javascript">
function load(){
document.getElementById("id_email").placeholder = "Enter Email";
}
</script>
{% endblock %}
尽管添加了自定义模板,但模板仍未呈现。默认的django重置表单是可见的。
答案 0 :(得分:0)
您正在检查相关视图的 django.contrib.auth.views.password_reset ,但您应该在views.py中创建一个视图,以便拥有自定义密码重置表单。
因此,在应用的views.py中创建类似
的类@login_required
def change_password(request):
try:
if request.POST:
form = Change_password_form(request.POST)
user = request.user.username
old_password = request.POST['old_password']
new_password1 = request.POST['new_password1']
new_password2 = request.POST['new_password2']
if form.is_valid():
if new_password1 == new_password2 and old_password != new_password1:
u = User.objects.get(username=user)
u.set_password(new_password1)
u.save()
return render(request, '../templates/password_change_done.html')
else:
form = Change_password_form()
return render(request, '../templates/password_change_form.html', {'form':form})
else:
form = Change_password_form()
return render(request, '../templates/password_change_form.html', {'form':form})
except:
return render(request, '../templates/password_change_form.html', {'form':form})
class Change_password_form(forms.Form):
old_password = forms.CharField(widget=forms.PasswordInput(attrs={'placeholder': 'Old Password'}), required=True)
new_password1= forms.CharField(widget=forms.PasswordInput(attrs={'placeholder': 'New Password'}), required=True)
new_password2= forms.CharField(widget=forms.PasswordInput(attrs={'placeholder': 'Confirm New Password'}), required=True)
action = forms.CharField(label="action", required=True)
def clean(self):
cleaned_data = super(Change_password_form, self).clean()
old_password = self.cleaned_data.get('old_password')
new_password1 = self.cleaned_data.get('new_password1')
new_password2 = self.cleaned_data.get('new_password2')
action = self.cleaned_data.get('action')
return self.cleaned_data
在 urls.py
中添加以下网址格式url(r&#39; ^ / change-password / $&#39;,&#39; appname.views.change_password&#39;,name =&#34; change-password&#34;),