I am following Corey Schafer Video lecture >> Password Reset Email
urls.py
from django.contrib import admin
from django.contrib.auth import views as auth_views
from django.urls import path, include
from users import views as user_views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('blog.urls')),
path('profile/', user_views.profile, name='profile'),
path('register/', user_views.register, name='register'),
path('login/', auth_views.LoginView.as_view(template_name='users/login.html'), name='login'),
path('logout/', auth_views.LogoutView.as_view(template_name='users/logout.html'), name='logout'),
path('password-reset/', auth_views.PasswordResetView.as_view(
template_name='users/password_reset.html'), name='password_reset'),
path('password-reset/done/', auth_views.PasswordResetDoneView.as_view(
template_name='users/password_reset_done.html'), name='password_reset_done'),
path('password-reset-confirm/<uidb64>/<token>/',
auth_views.PasswordResetConfirmView.as_view(
template_name='users/password_reset_confirm.html'),
name='password_reset_confirm'),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
AND CREATED 3 HTML FILE for these routes
but as per his lecture >> then he hit button (request reset password) he's getting error like noReverseMatch Reverse for 'password_reset_confirm' bla bla bla
then he created another route to handle this which is ('password-reset-confirm///')
but in my case
when i hit button request reset pasword it throw me to this route
"password-reset/done/" (with no error ,no email has been sent )
settings.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = "smtp.gmail.com"
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = config.EMAIL
EMAIL_HOST_PASSWORD = config.PASS
For those who struggled like me with this email sending tutorial: If you don't receive any email and don't get any error, it seems like Django won't send an email if the email address you submit in the form doesn't match the email registered for the user in the DB (specified in the profile and admin pages).
In other words, I had no errors when I was reseting the passowrd but I wasn't receiving any emails neither. After a few hours struggeling, I just changed user email adress so that it matches the one I wanted to send email to and it worked. (Ack to Elijah Mayorov in utube comment)
Related
In my Django Rest Framework, the users request to reset the password and when the email is received everytime the link is clicked it shows a message Password reset unsuccessful The password reset link was invalid, possibly because it has already been used. Please request a new password reset.
here is what I have tried API urls.py
app_name = 'api'
router = routers.DefaultRouter()
router.register(r'users', UserViewSet, basename='user')
urlpatterns = [
path('', include(router.urls)),
path('dj-rest-auth/', include('dj_rest_auth.urls')),
path('dj-rest-auth/registration/', include('dj_rest_auth.registration.urls')),
path('token/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
path('token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
# path('password_reset/',PasswordResetView.as_view(), name='password_reset'),
# path('password_reset_confirm/<uidb64>/<token>/', PasswordResetConfirmView.as_view(), name='password_reset_confirm'),
]
here is the users app urls.py if required:
app_name = 'users'
urlpatterns = [
path('password-reset/', auth_views.PasswordResetView.as_view(template_name='users/password_reset.html', success_url=reverse_lazy('users:password_reset_done')), name='password_reset'),
path('password-reset/done/', auth_views.PasswordResetDoneView.as_view(template_name='users/password_reset_done.html'),name='password_reset_done'),
path('password-reset-confirm/<uidb64>/<token>/',auth_views.PasswordResetConfirmView.as_view(template_name='users/password_reset_confirm.html',success_url=reverse_lazy('users:password_reset_done'),post_reset_login=True),name='password_reset_confirm',),
path('password-reset-complete/', auth_views.PasswordResetCompleteView.as_view(template_name='users/password_reset_complete.html'),name='password_reset_complete'),
]
My question is: Why do I keep receiving invalid links and how can I fix it?
In different questions I got answers to add the commented paths but still did not work. Any suggestions on how to fix it ?
When I log in to the Django project and then press the back button or enter the login page url, I am getting the Login page again even when the user is authenticated. I have tried adding the redirect_authenticated_user as in the code below:
from django.contrib import admin
from django.urls import path, include
from django.contrib.auth.decorators import login_required
from django.contrib.auth import views as auth_views
from django.conf.urls import url
admin.autodiscover()
admin.site.login = login_required(admin.site.login)
urlpatterns = [
path('admin/', admin.site.urls),
path('accounts/', include('django.contrib.auth.urls')),
path('accounts/login/', auth_views.LoginView.as_view(redirect_authenticated_user=True), name='login'),
]
Not sure if I have implemented something incorrectly.
I think you need to swap the order of your url patterns like this:
urlpatterns = [
path('admin/', admin.site.urls),
path('accounts/login/', auth_views.LoginView.as_view(redirect_authenticated_user=True), name='login'),
path('accounts/', include('django.contrib.auth.urls')),
]
When you try to access an endpoint, Django matches it with the first suitable url in your urlpatterns (In your case it was default django login with no parameters passed)
I wanna sent a email to users account to reset password. But whenever I enter the send it causes an error.
TimeoutError at /accounts/reset_password/
[WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
I do not know why I am having a lot of issue in doing reset password.
Things which I have done or try
I already enable "less secure" apps in my gmail account.
I was also having a issue in password_reset_email.html
and for that I use
email_template_name=accounts/registration/password_reset_email.html
and after adding this the error was TemplateDoesNotExist. And then I make a dir in my accounts app and add a password_reset_email.html
Here is code. any help please
urls.py
from django.urls import path, reverse_lazy
from . import views
from django.contrib.auth import views as auth_views
app_name = 'accounts'
urlpatterns = [
path('register/', views.register, name='register'),
path('signin/', views.signin, name='signin'),
path('logout/', views.logout, name='logout'),
path('user_profile/', views.edit_profile, name='edit_profile'),
path('user_password/', views.change_password, name='change_password'),
# Reset password section
path('reset_password/',
auth_views.PasswordResetView.as_view(
template_name='accounts/password_reset.html',
success_url=reverse_lazy('accounts:password_reset_done'),
email_template_name='accounts/registration/password_reset_email.html'
),
name='reset_password'),
path('reset_password_sent/',
auth_views.PasswordResetDoneView.as_view(
template_name='accounts/password_reset_sent.html',
),
name='password_reset_done'),
path('reset_confirm/<uidb64>/<token>',
auth_views.PasswordResetConfirmView.as_view(
template_name='accounts/password_reset_form.html',
success_url=reverse_lazy('accounts:password_reset_complete')
),
name='password_reset_confirm'),
path('rest_password_complete/',
auth_views.PasswordResetCompleteView.as_view(
template_name='accounts/password_reset_done.html',
),
name='password_reset_complete')
]
settings.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = '*******'
EMAIL_HOST_PASSWORD = '*******'
from django.urls import path, NoReverseMatch
from django.conf import settings
from django.contrib.auth import views as auth_views
from django.urls import path, include
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('login.html', views.login2, name='login2'),
path('register.html', views.register, name='register'),
path('logoutUser', views.logoutUser, name='logoutUser'),
path('music.html', views.music, name='music'),
path('profile.html', views.profile, name='profile'),
path('reset_password/',
auth_views.PasswordResetView.as_view(template_name="password_reset.html"),
name="reset_password"),
path('reset_password_sent/',
auth_views.PasswordResetDoneView.as_view(template_name="password_reset_sent.html"),
name="password_reset_done"),
path('reset/<uidb64>/<token>/',
auth_views.PasswordResetConfirmView.as_view(template_name="password_reset_form.html"),
name="password_reset_confirm"),
path('reset_password_complete/',
auth_views.PasswordResetCompleteView.as_view(template_name="password_reset_done.html"),
name="password_reset_complete"),
path('oauth/', include('social_django.urls',namespace='social')),
]
so I'm using this default password reset in Django and l get to send the email and receive it but the problem is that the reset link that will redirect me to the form is broken the port is missing, and if l fix the port in the URL after l click it works and takes me to the password_reset_confirm
this is the reset link l receive
http://localhost/reset/MQ/ac5zx3-f826598ef65286489c632fd68ecb1175/
I'm trying to use allauth and rest-auth in my project and try to use the built-in function in allauth to do email verification but this what I get :
and here is my code
settings.py
ACCOUNT_EMAIL_VERIFICATION = 'mandatory'
ACCOUNT_EMAIL_REQUIRED = True
urls.py
urlpatterns = [
re_path(r'^', include('rest_auth.urls')),
re_path(r'^registration/', include('rest_auth.registration.urls')),
]
I found the solution, that I have to add URL to be able to make a post request to the backend to send email then URL with regex which has the token that will verify the account and URLs and add URL for login with name account_login and URL for register with name account_signup and be like this :
from rest_auth.registration.views import VerifyEmailView, RegisterView
urlpatterns = [
path('', include('rest_auth.urls')),
path('login/', LoginView.as_view(), name='account_login'),
path('registration/', include('rest_auth.registration.urls')),
path('registration/', RegisterView.as_view(), name='account_signup'),
re_path(r'^account-confirm-email/', VerifyEmailView.as_view(),
name='account_email_verification_sent'),
re_path(r'^account-confirm-email/(?P<key>[-:\w]+)/$', VerifyEmailView.as_view(),
name='account_confirm_email'),
]
I had the same issue but I already had set up the URL for the email confirmation but I forgot about the name parameter it is mandatory
from django.conf.urls import url, include
from dj_rest_auth.registration.views import VerifyEmailView
urlpatterns = [
url('auth/', include('dj_rest_auth.urls')),
url('auth/registration/', include('dj_rest_auth.registration.urls')),
url('auth/account-confirm-email/', VerifyEmailView.as_view(), name='account_email_verification_sent'),
]
ยดยดยด