So I changed the name of my registration template from user_form.html to register.html. I've also changed it in the urls.py file to reflect the new name, however when I try to go to the register page I get the following:
Exception Type: TemplateDoesNotExist
Exception Value:web/user_form.html
Template-loader postmortem
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
Using loader django.template.loaders.app_directories.Loader:
../swl/root/lib/pythpy2.7.egg/django/contrib/admin/templates/web/user_form.html (File does not exist)
../django/contrib/auth/templates/web/user_form.html (File does not exist)
../templates/web/user_form.html (File does not exist)
..tastypie/templates/web/user_form.html (File does not exist)
../bootstrap3/templates/web/user_form.html (File does not exist)
../web/templates/web/user_form.html (File does not exist)
Why would the template loader still be looking for the old filename?
edit: this is my url for register:
url(r'register/?$',
UserCreate.as_view(),
{
'template_name':'accounts/register.html'
},
name = 'register'),
Try to replace all user_form.html to registration.html in all files. You can use Find/replace in files feature (or something similar) of your text editor or IDE.
Change url to:
url(r'register/?$',
UserCreate.as_view(template_name='accounts/register.html'),
name = 'register')
The class-based views will attempt to use a default template name when no name is explicitly declared. (The CreateView uses (appname)/(model)_form.html) Your choices are:
Name your template using the default name
Specify the template in the URL (as in Vladimir's answer)
Specify the template name in the view code like so:
class UserCreate(CreateView):
template_name = "accounts/register.html"
Hope this helps
Restart the apache server.
For Ubuntu server 20:
/etc/init.d/apache2 restart
Related
I am a beginner in the Django framework. I created my project created my app and test it, it work fine till I decided to add a template. I don't know where the error is coming from because I follow what Django docs say by creating folder name templates in your app folder creating a folder with your app name and lastly creating the HTML file in the folder.
NOTE: other routes are working fine except the template
Please view the screenshot of my file structure and error below.
File Structure
ERROR
TemplateDoesNotExist at /blog/
Blog/index
Request Method: GET
Request URL: http://127.0.0.1:8000/blog/
Django Version: 4.0.1
Exception Type: TemplateDoesNotExist
Exception Value:
Blog/index
Exception Location: C:\Python39\lib\site-packages\django\template\loader.py, line 19, in get_template
Python Executable: C:\Python39\python.exe
Python Version: 3.9.4
Python Path:
['C:\\Users\\Maxwell\\Desktop\\Django\\WebApp',
'C:\\Python39\\python39.zip',
'C:\\Python39\\DLLs',
'C:\\Python39\\lib',
'C:\\Python39',
'C:\\Users\\Maxwell\\AppData\\Roaming\\Python\\Python39\\site-packages',
'C:\\Python39\\lib\\site-packages',
'C:\\Python39\\lib\\site-packages\\win32',
'C:\\Python39\\lib\\site-packages\\win32\\lib',
'C:\\Python39\\lib\\site-packages\\Pythonwin']
Server time: Sun, 23 Jan 2022 12:04:18 +0000
Template-loader postmortem
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.app_directories.Loader: C:\Users\Maxwell\Desktop\Django\WebApp\Blog\templates\ Blog\index (Source does not exist)
django.template.loaders.app_directories.Loader: C:\Python39\lib\site-packages\django\contrib\admin\templates\ Blog\index (Source does not exist)
django.template.loaders.app_directories.Loader: C:\Python39\lib\site-packages\django\contrib\auth\templates\ Blog\index (Source does not exist)
App/views.py
from django.http import HttpResponse
from django.shortcuts import render
# Create your views here.
def index(request,name):
return HttpResponse(f'Hello {name}')
def html(request):
return render(request,' Blog/index.html')
It seems that you render the template with Blog/index, but you need to specify the entire file name, so Blog/index.html and without a leading (or trailing) space:
def html(request):
return render(request, 'Blog/index.html')
if #Willem van Onsem answer didn't work
create a templates folder in the main directory 'in the same level as WebApp'
then inside it create folders for each app
in your settings.py you will find TEMPLATES option
TEMPLATES = [
{
...
'DIRS': [os.path.join(BASE_DIR,'templates')],#add this line
...
]
and when you want to use a template
return render(request,"template_folder/template_name.html")
I faced this situation too, and went through most of the answers here talking about the path, and editing the 'templates' function in settings.py.
In the end, it was because I forgot to add my app to the installed apps section in the settings.py file. I saw this in the docs at https://docs.djangoproject.com/en/4.0/intro/tutorial03/#write-views-that-actually-do-something
The server is working fine, however while using extend for this 'home.html' file unable to link the template :
In 'home.html':
{ % extends "/template1/personal/header.html" % }
While loading localhost:
Django tried loading these templates, in this order:
`Using engine django:
django.template.loaders.app_directories.Loader: /usr/local/lib/python2.7/dist-packages/django/contrib/admin/templates/ffgg/home.html (Source does not exist).
django.template.loaders.app_directories.Loader: /usr/local/lib/python2.7/dist-packages/django/contrib/auth/templates/ffgg/home.html (Source does not exist)`
1) Check what is in TEMPLATE_DIRS Variable in settings.py. That is the folder Django looks up in every app in order to find your template.
2) Try remove the leading slash:
{% extends 'template1/personal/header.html' %}
3) if template1 is in TEMPLATE_DIRS, then 'app/template.html' would be enough
{% extends 'personal/header.html' %}
Read on:
https://tutorial.djangogirls.org/en/template_extending/
https://docs.djangoproject.com/en/1.7/topics/templates/
I have some third-part library, templates from that library is diplayed by default.
I want to replace layout template, from which all other start inheritance.
So, in my local app I create new layout.html file, which contents
{% extends 'library:layout.html' %}
But in that case I've got
TemplateDoesNotExist at /test-url/
test.html
But still Template-loader postmortem display, that it can find file:
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
/path/to/app/templates/test.html (File does not exist)
Using loader django.template.loaders.app_directories.Loader:
/path/to/library/app1/templates/test.html (File does not exist)
/path/to/library/app2/templates/test.html (File does not exist)
/path/to/library/app3/templates/test.html (File exists)
So, in library app3 Django found template, but for some reason do not use it. Can you help with this issue?
Thanks.
Make sure that there isn't another missing template that is causing the error.
Your extends tag looks wrong. I would expect to see library/layout.html, not library:layout.html
{% extends 'library/layout.html' %}
I am having a strange issue using the built in password_rest view of django(1.8). In the documentation they say that the template name defaults to registration/password_reset_form.html. Keeping that in mind, I didn't give any template name in my urls.py, thinking that django will load the default template.Here is my url for the password reset
urls.py
url(r'^password_reset$', auth_views.password_reset, name='password_reset'),
When I click the forgot password link,
<p><a href={% url 'password_reset' %}> Forgot your password?</a></p>
which executes the password_rest view, I am getting Template DoesNotExist error and below is the error message
TemplateDoesNotExist at /accounts/password_reset
registration/password_reset.html
Template-loader postmortem
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
/home/vinothkumar/github/djtest/templates/registration/password_reset.html (File does not exist)
Using loader django.template.loaders.app_directories.Loader:
/home/vinothkumar/github/djtest/venv_new/lib/python3.4/site-packages/django/contrib/admin/templates/registration/password_reset.html (File does not exist)
/home/vinothkumar/github/djtest/venv_new/lib/python3.4/site-packages/django/contrib/auth/templates/registration/password_reset.html (File does not exist)
/home/vinothkumar/github/djtest/accounts/templates/registration/password_reset.html (File does not exist)
my question is why the template loader is looking for /registration/password_reset.html instead of loading the default /registration/password_reset_form.html?
I tried the same approach for password_reset_done, in this case it loads the default template /registration/password_reset_done.html. and everything there works fine.
I am trying to load a register template for my first django site,
I put register.html file in /home/Desktop/projects/purple/purple/templates/registration.html
here but there is a error that is say to there is no file I did not understant that's why? can anyone have a idea?
Request Method: GET
Request URL: http...:127.0.0.1:8000/registration/
Django Version: 1.4
Exception Type: TemplateDoesNotExist
Exception Value: registration.html
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
/home/Desktop/projects/purple/purple/templates/registration.html (File does not exist)
Using loader django.template.loaders.app_directories.Loader:
/usr/local/lib/python2.7/dist-packages/django/contrib/auth/templates/registration.html (File does not exist)
/usr/local/lib/python2.7/dist-packages/django/contrib/admin/templates/registration.html (File does not exist)
Using loader django.template.loaders.eggs.Loader:
Given that the exception copy and pasted refers to:
/home/Desktop/projects/purple/purple/templates/registration.html
and can't find it... all of your problems lie in whether or not registration.html exists at the directory.
There are 2 possibilities:
File doesn't exist
cd into that exact directory and find out if it does.
Permissions
ls -lh /home/Desktop/projects/purple/purple/templates/registration.html
Make sure it's readable.
chmod 644 /home/Desktop/projects/purple/purple/templates/registration.html
do you have Registration directory inside Template directory??
if so then include this is your settings.py .
TEMPLATE_DIRS = (
BASE_DIR +'/Templates',
BASE_DIR +'/registration',
)
Because django is looking for registration.html in your Template directory instead of /template/registration.
see the order you mentioned in the question. watch the path.