I used Django's auth_views.password_change to allow a user to change their password. It uses a custom template and it works well, but the problem I'm having is that it redirects to Django's password change successful admin template instead of the custom template I declared.
urls.py
from django.conf.urls import url
from django.contrib.auth import views as auth_views
from . import views
app_name = 'users'
urlpatterns = [
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^add-user/$', views.CreateUserView.as_view(), name='add-user'),
url(r'^search/$', views.UserSearchView.as_view(), name='search-users'),
url(r'^login/$', views.LoginView.as_view(), name='login'),
url(r'^logout/$', views.LogoutView.as_view(), name='logout'),
url(r'^(?P<pk>\d+)/settings/update$', views.UpdateAccountView.as_view(), name='update-account'),
url(r'^settings/change/$', auth_views.password_change, {'template_name': 'users/forms/change-password.html'},
name='change-password'),
url(r'^settings/change-done/$', auth_views.password_change_done,
{'template_name': 'users/forms/change-password-done.html'}, name='change-password-done'),
url(r'^(?P<pk>\d+)/delete-user/$', views.DeleteUserView.as_view(), name='delete-user'),
Any Ideas what I missed?
In INSTALLED_APPS ensure that your app with the custom templates is specified before admin.
Django searches for the template in order.
You should use:
url(r'^settings/change/$', auth_views.password_change, {'template_name': 'users/forms/change-password.html',post_change_redirect:'change-password-done'},name='change-password'),
Without post_change_redirect default template will be loaded in that case.
Quite long since this question was asked, but the above answers didn't work for me and this did. In urls.py:
Make sure this import is there:
from django.contrib.auth.views import ..., password_change_done, ...
Then remove url(r'^', include('django.contrib.auth.urls')) from the URLs and override the password_change_done view with your custom template. Again, I also had the above suggestions already implemented in my code.
Related
Django newbie here. I am just trying to make a password change form using Django authentication system. I used my own views for login and logout functionality. I read the docs
and for password change form as mentioned, I made a registration folder inside my templates directory and made a template html file named password_change_form.html , but when I go to the url http://127.0.0.1:8000/accounts/password_change , I keep getting the error TemplateDoesNotExist at /accounts/login/ . Here is my app structure:
and my urls.py file:
from django.contrib import auth
from django.urls import path,include
from django.contrib.auth import views as auth_views
from . import views
urlpatterns = [
path('',views.home_page,name='home_page'),
path('register',views.registration_view,name='registration_view'),
path('login',views.login_view,name='login_view'),
path('logout',views.logout_view,name='logout_view'),
path('accounts/password_change/', auth_views.PasswordChangeView.as_view(), name='password_change'),
]
What and why is this happening? Please help. I would appreciate it.
It is recommended that you view your django.contrib.auth files to find out more easily. Django default template for resetting the password is templates/registration/password_reset_form.html. As a result, your filename should be password_reset_form.html. For an easier understanding of this, as I mentioned, you can look at the django.contrib.auth files.
This urls.py file is the main django admin system:
from django.contrib.auth import views
from django.urls import path
urlpatterns = [
path('login/', views.LoginView.as_view(), name='login'),
path('logout/', views.LogoutView.as_view(), name='logout'),
path('password_change/', views.PasswordChangeView.as_view(), name='password_change'),
path('password_change/done/', views.PasswordChangeDoneView.as_view(), name='password_change_done'),
path('password_reset/', views.PasswordResetView.as_view(), name='password_reset'),
path('password_reset/done/', views.PasswordResetDoneView.as_view(), name='password_reset_done'),
path('reset/<uidb64>/<token>/', views.PasswordResetConfirmView.as_view(), name='password_reset_confirm'),
path('reset/done/', views.PasswordResetCompleteView.as_view(), name='password_reset_complete'),
]
Because the views of the Django registration system are written in Class Based format, you need to put them in .as_view() format.
You can copy all urls related to password reset from here and put it in your urls.py and continue personalization.
Whenever I try to log in with username and password this happens:
my project name is:corey_schafer
app name for user stuff is: users
projects urls.py:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('blog.urls')),
path('register/', include('users.urls'))
]
users urls.py :
from django.urls import path
from . import views
urlpatterns = [
path('', views.register, name='register'),
path('signup', views.signup, name='signup'),
path('login/', views.login, name='login'),
path('logout/', views.logout, name='logout'),
]
users views.py:
[![enter image description here][2]][2]
if views.py image not showing please go here:https://i.stack.imgur.com/auZxo.png
if any other file is needed to solve this problem please let me know
You url basically needs to have only register/login/ path rather than two /logins.
Your path defined is first go to register.urls and inside that go match and go to login as per the URLs files mentioned above. Whereas the path in the question that you are trying to access says go to register then login and login again which isn't available, thus 404!
You need to add app_name parameter in users urls.py in order to be able to have redirect automatically use reverse to correct view under the hood.
I'm new to Django and I'm trying to implement the django-login-required-middleware in my project to be able to direct all the users who are not logged in to the index page with login view.
I Installed the pip install django-login-required-middleware, added login_required in my INSTALLED_APPS settings and added login_required.middleware.LoginRequiredMiddleware in my MIDDLEWARE. Then in my settings I ignore the views that I want to display to users even when They're not logged in.
settings.py
LOGIN_REQUIRED_IGNORE_VIEW_NAMES = [
'index',
'register'
]
However, when I run the server, I get the error
Not Found: /accounts/login/
[22/Jan/2020 12:27:56] "GET /accounts/login/?next=/ HTTP/1.1" 404 4417
and in my browser:
Request Method: GET
Request URL: http://127.0.0.1:8000/accounts/login/?next=/
It seems like it automatically directs me to the accounts, even though my app is called movies_app and not accounts. Anyone knows how to fix this? Thank you very much!
urls.py
from django.urls import path
from django.conf import settings
from django.conf.urls.static import static
from .views import editprofile
from . import views
app_name = 'movies_app'
urlpatterns = [
path('', views.login, name='login'),
path('browse/', views.index, name='index'),
path('register/', views.register, name='register'),
path('movies/', views.allMovies, name='allMovies'),
path('movies/<int:pk>/', views.movie, name='movie'),
path('movies/<int:pk>/rate', views.addRating, name='rate'),
path('my-list/', views.myMovies, name='my-list'),
path('my-list/<int:pk>/delete', views.deleteFavoriteMovie, name='favorite-movie-delete'),
path('profile/', views.profile, name='register'),
path('editprofile/', views.editprofile, name='editprofile'),
path('logout/', views.logout, name='logout'),
path('movie-video', views.movieVideo, name='movie-video')
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Your middleware is working and redirecting you to the default login page, /accounts/login/.
To customise the default login page, add LOGIN_URL to your settings, e.g.
LOGIN_URL = '/login/'
Finally, you have app_name = 'movies_app', so you should include this when referring to URL patterns from this app. For example, yourLOGIN_REQUIRED_IGNORE_VIEW_NAMES` should be:
LOGIN_REQUIRED_IGNORE_VIEW_NAMES = [
'movies_app:index',
'movies_app:register'
]
Okay... let's try to explain things clearly. I've used Python Django to create a dynamic webpage/web-app. After completing the website I have published it using DigitalOcean and have successfully attached my purchased domain name to the name server of DigitalOcean. When I access my website, ordinanceservices.com, i get an error 404; however, if I type ordinanceservices.com/home it works as it should and displays the home page. How, by editing the python files, can I have it to where ordinanceservices.com will display the home page as opposed to error 404? I feel like there's something that I am doing that is fundamentally wrong regarding my .urls structure and thus a rewrite/redirect in the nginx config should not be necessary.
Here is the specific error:
Page not found (404)
Request Method: GET
Request URL: http://ordinanceservices.com/
Using the URLconf defined in django_project.urls, Django tried these URL patterns, in this order:
^admin/
^ ^home/ [name='home']
^ ^contact/ [name='contact']
^ ^services/ [name='services']
The current URL, , didn't match any of these.
I somewhat understand what is happening here though I do not know how to fix this. Next I will provide my .urls files for each folder that contains such:
/django_project urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('company.urls')),
)
/company urls.py
from django.conf.urls import url, include
from . import views
urlpatterns = [
url(r'^home/', views.home, name='home'),
url(r'^contact/', views.contact, name='contact'),
url(r'^services/', views.services, name='services'),
]
/company views.py
from django.shortcuts import render
def home(request):
return render(request, 'company/home.html')
def contact(request):
return render(request, 'company/contact.html')
def services(request):
return render(request, 'company/services.html')
What I am aiming to do, without needing to redirect the main URL using the nginx config files to do so, is to edit my urls and views structure of my Python files to ensure that the normal URL, ordinanceservices.com, will actually display a page; preferably the home page of my webpage.
I have a hunch that it has to do with the fact that I do not have a views.index for the r'^admin/' to reach to. I am not certain but I have been trying to figure this out for hours. Does anyone have a clue what I can do to fix this?
You haven't defined anything at the root url. Add one more line to your company urls.py so it becomes
//company urls.py
from django.conf.urls import url, include
from . import views
urlpatterns = [
url(r'^$', views.home, name='home'),
url(r'^home/', views.home, name='home'),
url(r'^contact/', views.contact, name='contact'),
url(r'^services/', views.services, name='services'),
]
First of all, check if you have added www.yourdomain.com or yourdomain.com to ALLOWED_HOST = ['www.yourdomain.com','yourdomain.com'] in your settings.py
and then in your company/urls.py do this
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.home, name='home'),
url(r'^contact/$', views.contact, name='contact'),
url(r'^services/$', views.services, name='services'),
]
and in your main urls.py add this code
from django.conf.urls import url,include
from django.contrib import admin
admin.autodiscover()
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('company.urls')),
]
I followed http://blog.narenarya.in/right-way-django-authentication.html in order to add a user authentication in my Django project but when I migrate an error occured in my project urls.py but I didn't find it !!!
newsite/newsite/urls.py
from django.conf.urls import include, url
from django.contrib import admin
from aps import views
from mail import views
from log import views
from django.contrib.auth import views
from log.forms import LoginForm
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^aps/', include('aps.urls')),
url(r'^mail/', include('mail.urls')),
url(r'^log/', include('log.urls')),
url(r'^login/$', views.login, {'template_name': 'login.html', 'authentication_form': LoginForm},
url(r'^logout/$', views.logout, {'next_page': '/login'}),
]
You're missing the last ).
url(r'^login/$', views.login, {'template_name': 'login.html', 'authentication_form': LoginForm}),
I highly suggest you go back to the link that you provided as your source. You've miscopied some of the code. For example this line from the example provided:
url(r'^admin/', include(admin.site.urls)),
You did not provide the error message/trace you received, so of the couple of problems it could be, its impossible to say which one is giving you the error you are currently receiving -- but again, compare your code to the example for errors (including in indentation). If it still doesn't work, provide the error message you receive