python - auth views not rendering templates in django -
this entire urls.py file have designed custom templates reset password functionality
from django.conf.urls import include, url django.contrib import admin 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, }), ]
i looked @ django documentation @ https://docs.djangoproject.com/en/1.8/topics/auth/default/ , there tried add custom templates this
url( '^password_reset/', 'django.contrib.auth.views.password_reset', {'template_name': 'change-password.html'} )
i'm getting deafult django ui have custom template not showing up
{% 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 password</button> </form> <script type="text/javascript"> function load(){ document.getelementbyid("id_email").placeholder = "enter email"; } </script> {% endblock %}
in spite of adding custom templates templates not getting rendered. default django reset forms visible.
you checking django.contrib.auth.views.password_reset related view, should create view in views.py in order have custom password reset form.
so in views.py of app create class like
@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 , 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
add following url pattern in urls.py
url(r'^/change-password/$', 'appname.views.change_password', name="change-password"),
Comments
Post a Comment