How to send html-styled password reset email with django - python

I created an html-styled version of a password reset email to be set with django 4.0 located at 'registration/html_password_reset_email.html'. From other stackoverflows, I learned I needed to add the html_email_template_name parameter for the html version of the email to be sent. However, even with the below code, it is just the text version of the file that is being sent ('registration/password_reset_email.html'). It is definitely fining the registration/password_reset_email.html file, because edits I make to it are successfully emailed, but I can't get it to send the html version. Hints on what I'm doing wrong?
from django.contrib import admin
from django.urls import include, path
from django.contrib.auth import views as auth_views
from django.views.generic.base import RedirectView
from django.urls import reverse
from . import views
urlpatterns = [
path('', views.homeview, name="homeview"),
path('dashboard/', include('dashboard.urls')),
path('admin/', admin.site.urls),
path('accounts/', include('django.contrib.auth.urls')),
path("register/", views.register_request, name="register"),
path('reset_password/', auth_views.PasswordResetView.as_view(
template_name='registration/password_reset_form.html',
html_email_template_name='registration/html_password_reset_email.html',
email_template_name='registration/password_reset_email.html',
), name="reset_password"), # Submit email form
path('reset_password_sent/', auth_views.PasswordResetDoneView.as_view(), name="password_reset_done"), # Email sent success message
path('reset/<uidb64>/<token>/', auth_views.PasswordResetConfirmView.as_view(), name="password_reset_confirm"), # Link to password reset form in email
path('reset_password_complete/', auth_views.PasswordResetCompleteView.as_view(), name="password_reset_complete"), # Password successfully changed message
]
This is part of my settings.py file
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent.parent
# Take environment variables from .env file
environ.Env.read_env(os.path.join(BASE_DIR, '.env'))
# Application definition
INSTALLED_APPS = [
'dashboard.apps.DashboardConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'crispy_forms',
'mysite',
'users',
'corsheaders',
]
AUTH_USER_MODEL = 'users.User'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
BASE_DIR resolves to: /Users/MYNAME/Documents/repos/begolden/mysite/templates
and my template files are at:
/Users/MYNAME/Documents/repos/begolden/mysite/templates/registration/html_password_reset_email.html
I tried setting EMAIL_BACKEND to django.core.mail.backends.console.EmailBackend and the text version in registration/password_reset_email.html (not the html version) prints out to console. I also tried changing the html file to just say "Hello World", in case the original html was malformed. The text registration/password_reset_email.html still prints to console (not the html file location).
I think it isn't actually using the file locations I'm providing, because when I change the email_template_name like below, it doesn't read the new text 'registration/test.html'. It still reads the text at the default location 'registration/password_reset_email.html '. I find this confusing though, because it IS finding my custom text at 'registration/password_reset_email.html', which seems to imply that my folder structure is correct but PasswordResetView just isn't using the argument names I am giving it??
path('password_reset/', auth_views.PasswordResetView.as_view(
html_email_template_name='registration/html_password_reset_email.html',
email_template_name='registration/test.html'
), name="password_result"),

You named your URL reset_password, while the name used by Django is password_reset. You have to use the same name, as it will be called by Django with reverse('reset_password'):
urlpatterns = [
...,
path('accounts/', include('django.contrib.auth.urls')),
...,
path('reset_password/', auth_views.PasswordResetView.as_view(
template_name='registration/password_reset_form.html',
html_email_template_name='registration/html_password_reset_email.html',
email_template_name='registration/password_reset_email.html',
), name="password_reset"), # <-
...,
]
Also, beware to keep this URL pattern after path('accounts/', include('django.contrib.auth.urls')), as the URL name will clash and the last one has the precedence.
When naming URL patterns, choose names that are unlikely to clash with other applications' choice of names. If you call your URL pattern comment and another application does the same thing, the URL that reverse() finds depends on whichever pattern is last in your project's urlpatterns list.

Related

trying to override allauth templates on a Django 3.2 app recognized in python 3.9

I'm working with ALLAUTH on Django 3.2. Most solutions I found are for Django 1, so I'm hoping to find something more up-to-date here.
I have the module/app installed, but I'm having problems overriding the templates with my own.
In settings.py:
INSTALLED_APPS = [
...
'Landing.apps.LandingConfig',
'allauth',
'allauth.account',
'allauth.socialaccount',
'allauth.socialaccount.providers.google'
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
BASE_DIR / 'templates'
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
# Already defined Django-related contexts here
# `allauth` needs this from django
'django.template.context_processors.request',
],
},
},
]
AUTHENTICATION_BACKENDS = [
# Needed to login by username in Django admin, regardless of `allauth`
'django.contrib.auth.backends.ModelBackend',
# `allauth` specific authentication methods, such as login by e-mail
'allauth.account.auth_backends.AuthenticationBackend',
]
Research Urls #The Project
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('', include('Landing.urls')),
path('admin/', admin.site.urls),
path('accounts/', include('allauth.urls')),
]
Landing/urls.py #app-level urls
from django.contrib import admin
from django.urls import path, include
from . import views
urlpatterns = [
path('', views.home, name='home'),
path('login/', views.LoginView.as_view(), name='account_login'),
path('signup/', views.SignupView.as_view(), name='account_signup')
]
Home.html
<...>
<body>
<H1>This is the homepage</H1>
<p>login</p>
<p>Create Account</p>
</body>
<...>
Note: account_login and account_signup are both in Landing urls and Landing Views
Landing Views
from django.shortcuts import render
from allauth.account.views import LoginView, SignupView
# Create your views here.
def home(request):
return render(request, 'Landing/home.html')
class LandingLogin(LoginView):
print('found Login View....')
template_name = 'authentication/login.html'
class LandingSignup(SignupView):
print('found Login View....')
template_name = 'authentication/account_signup.html'
My Tree
I can navigate to localhost:8000, and when the html comes up, two things occur:
links on home.html still point to allauth links
Landing/Home points to the custom template, but it still routes to the allauth page.
How can set the view, link, and route to the correct page?
Thanks!
In Landing/urls.py, login and signup still points to allauth views (allauth.account.views.LoginView, allauth.account.views.SignupView) and not the overridden views.
Can you try changing them from:
path('login/', views.LoginView.as_view(), name='account_login'),
path('signup/', views.SignupView.as_view(), name='account_signup')
to:
path('login/', views.LandingLogin.as_view(), name='account_login'),
path('signup/', views.LandingSignup.as_view(), name='account_signup')
If you want to override the HTML templates that are provided by allauth, you need to save your template files in templates/account/ directory instead of templates/authentication/. Then you won't need to use the template_name variable. Also make sure that the name of the HTML file corresponds to the page you want to override. For example if you want to override the login page, you need to save your template in the templates/account/ directory with the name login.html. You can take a look at the views in the allauth library to see what name you need to keep. Take a look at the documentation for more info.
And also as the answer given by #bdbd, you would need to change the view names in the urls.py file.

Django is loading template from the wrong app

I have a view under my elearning app named home(), which should load index.html from within the app's directory. Instead it loads an instance of index.html from a different app (symposium/templates/index.html). It should be loading it from (elearning/templates/index.html).
Can someone please explain why this is happening and how to fix it?
# Settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# Machina dependencies:
'mptt',
'haystack',
'widget_tweaks',
# Machina apps:
'machina',
'machina.apps.forum',
'machina.apps.forum_conversation',
'machina.apps.forum_conversation.forum_attachments',
'machina.apps.forum_conversation.forum_polls',
'machina.apps.forum_feeds',
'machina.apps.forum_moderation',
'machina.apps.forum_search',
'machina.apps.forum_tracking',
'machina.apps.forum_member',
'machina.apps.forum_permission',
# SCORM apps:
'accounts',
'symposium',
'elearning']
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,'templates'), MACHINA_MAIN_TEMPLATE_DIR,],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'machina.core.context_processors.metadata',
],
},
},
]
# root urls.py
from django.contrib import admin
from django.urls import path, include
from machina import urls as machina_urls
from accounts.views import CreateUser
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
path('forum/',include(machina_urls)),
path('admin/', admin.site.urls),
path('createuser',CreateUser.as_view()),
path('symposium/', include('symposium.urls')),
path('elearning/', include('elearning.urls')),
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
# elearning app urls.py
from django.urls import include, path
from . import views
app_name = 'elearning'
urlpatterns = [
path('', views.home, name='home'),
]
# app: elearning/views.py
from django.shortcuts import render
# Create your views here.
def home(request):
return render(request,'index.html',{})
# index.html location
elearning templates : ./elearning/templates/index.html
Django searches for a templates folder within each app for the relevant template to render. Looks like the first it found was index.html from within the symposium app.
The convention of directory structure for templates is to put your templates within another folder with the name of the app in order to avoid this exact situation with templates of the same name.
Move your index.html file from
/elearning/templates/index.html
to
/elearning/templates/elearning/index.html
and then change your home view function to
def home(request):
return render(request,'elearning/index.html',{})
and similarly do the same for the location of the index.html file in the symposium app.

Template file not found Django

I am trying to make a home page of a new website via Django.
My app name is 'blog', home page is home.html
I still receive the error template does not exist when I go to http://127.0.0.1:8000/blog/home/
I made sure I added 'blog' to my templates in settings.py and that I added the folder templates in the main directory as well as through blog/templates/blog/home.html
myproject/blog/views.py
from django.shortcuts import render
from django.http import HttpResponse
def home(request):
return render(request, 'blog/home.html')
myproject/blog/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('home/', views.home, name='home'),
]
myproject/settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog',
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'templates')
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
myproject/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('blog/', include('blog.urls')),
]
Do you see anything in my code which causes a problem? I receive the message in blog/views.py that "Template file 'blog' not found" on the line
return render(request, 'blog/home.html')
You are doing it wrong. You need to read the Django documentation carefully and try to understand whatever you read and implement the same step by step.
The url you have to hit is
http://127.0.0.1:8000/blog/home/
home.html will be rendered at this url.
You don't put html page name in the url
I have been looking for answers too and I tried everything but this worked for me on my windows machine. Adding 'r' before "templates" in os.path.join(BASE_DIR, 'templates') to look like this os.path.join(BASE_DIR, r'templates') resolved the error issue. Also my templates directory was in my project root along side the parent app and sub-apps.

I want to load a template from the templates folder but I get an error that says the included URLconf does not appear to have any patterns in it

I am trying to load a template in my templates/pages folder and get the error: django.core.exceptions.ImproperlyConfigured: The included URLconf '<module 'pages.urls' from 'D:\\django\\pages\\pages\\urls.py'>' does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.
I have tried putting the templates folder in both the project and the app directory but still get the same error.
In my settings.py I have:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
and:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'pages',
]
my urls.py file in the root project folder named pages_project looks like:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('pages.urls')),
]
and my urls.py in my app folder named pages looks like:
from django.urls import path
from . import views
path('', views.HomePageView.as_view(), name='home')
my views.py looks like:
from django.shortcuts import render
from django.views.generic import TemplateView
class HomePageView(TemplateView):
template_name= 'home.html'
I have a template file named home.html in the path pages/templates/pages/home.html and looks like:
<h1>Homepage</h1>
This doesn't have anything to do with templates.
As the error says, the included URLconf doesn't have any patterns in it. As you can see from the main urls.py, you need to define a list named urlpatterns which contains your patterns. So your pages urls.py should be:
urlpatterns = [
path('', views.HomePageView.as_view(), name='home')
]

Cant open the server correctly

Can you please help me, I have downloaded the project social. Then, I went to that folder using the command line, and typed migrate then runserver, I get these warning in my command line:
C:\django\social\social\urls.py:25: RemovedInDjango110Warning:
django.conf.urls.patterns() is deprecated and will be removed in Django
1.10. Update your urlpatterns to be a list of django.conf.urls.url()
instances instead.
url(r'^checkuser/$', views.checkuser, name='checkuser'),
C:\Program Files\Python35\lib\site-packages\django\template\utils.py:37:
RemovedInDjango110Warning: You haven't defined a TEMPLATES setting. You must
do so before upgrading to Django 1.10. Otherwise Django will be unable to
load templates.
"unable to load templates.", RemovedInDjango110Warning)
C:\django\social\mysite\urls.py:6: RemovedInDjango110Warning:
django.conf.urls.patterns() is deprecated and will be removed in Django
1.10. Update your urlpatterns to be a list of django.conf.urls.url()
instances instead.
url(r'^admin/', include(admin.site.urls)),
AFter I try to go to that server, I get 404 message. Here is the script for social/urls.py:
from django.conf.urls import patterns, url
from social import views
urlpatterns = patterns('',
# main page
url(r'^$', views.index, name='index'),
# signup page
url(r'^signup/$', views.signup, name='signup'),
# register new user
url(r'^register/$', views.register, name='register'),
# login page
url(r'^login/$', views.login, name='login'),
# logout page
url(r'^logout/$', views.logout, name='logout'),
# members page
url(r'^members/$', views.members, name='members'),
# friends page
url(r'^friends/$', views.friends, name='friends'),
# user profile edit page
url(r'^profile/$', views.profile, name='profile'),
# messages page
url(r'^messages/$', views.messages, name='messages'),
# Ajax: check if user exists
url(r'^checkuser/$', views.checkuser, name='checkuser'),
)
Change
urlpatterns = patterns('',
...
)
to
urlpatterns = [
...
]
to get rid of the urlpatterns warnings. urlpatterns have to be a list in newer Django versions. You can also remove the patterns import.
And add this to your settings.py:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

Categories