I have my folders set up like this:
~/projects/myproject/
~/.envs/myproject/
My app folders look like this:
myapp/
├── __init__.py
├── admin.py
├── forms.py
├── migrations/
├── models.py
├── tests/
├── templates/
├── urls.py
├── views.py
I just implemented a custom password reset view, because I wanted to use my own templates. The problem is, that the password reset view does not find my template. Here is my view:
class CustomPasswordResetConfirmView(views.PasswordResetConfirmView):
success_url = reverse_lazy("accounts:password_reset_complete")
template_name = "accounts/registration/password_reset_confirm.html"
And here is the error message that I get:
TemplateDoesNotExist at /auth/reset/MQ/set-password/
accounts/registration/password_reset_confirm.html.
Request Method: GET
Request URL: http://localhost:8000/auth/reset/MQ/set-password/
Django Version: 1.11.13
Exception Type: TemplateDoesNotExist
Exception Value:
accounts/registration/password_reset_confirm.html.
Exception Location: /Users/jan/.envs/myproject/lib/python3.6/site-packages/django/template/loader.py in select_template, line 53
Python Executable: /Users/jan/.envs/myproject/bin/python
Python Version: 3.6.5
Python Path:
['/Users/jan/Dev/Misc Tutorial/myproject',
'/Users/jan/.envs/myproject/lib/python36.zip',
'/Users/jan/.envs/myproject/lib/python3.6',
'/Users/jan/.envs/myproject/lib/python3.6/lib-dynload',
'/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6',
'/Users/jan/.envs/myproject/lib/python3.6/site-packages']
How can I get Django to find my custom templates? From the error I can tell that he looks in my virtual environments.
I tried specifying the templates path like this:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
[os.path.join(BASE_DIR, 'templates')]
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.contrib.auth.context_processors.auth',
'django.template.context_processors.debug',
'django.template.context_processors.i18n',
'django.template.context_processors.media',
'django.template.context_processors.static',
'django.template.context_processors.tz',
'django.contrib.messages.context_processors.messages',
],
},
},
]
The code for this I got from the Django documentation. Unfortunately this doesn't help. If you have any idea how I can fix this, I would be very thankful!
(On a related note, I also tried to use django-admin instead of manage.py, because the official docs recommend it. But I couldn't set the DJANGO_SETTINGS_MODULE env variable so that it works. Maybe this problem is related.)
EDIT: The app for the account related stuff is called accounts. In it is a folder called templates, holding various other folders, one is called 'registration'.
Kinda solved.
Circumvented the django-admin problem by using manage.py just as pydanny suggested.
And I just had my templates configured wrongly.
Here is the new view I used:
class CustomPasswordResetConfirmView(views.PasswordResetConfirmView):
success_url = reverse_lazy("accounts:password_reset_complete")
template_name = "registration/password_reset_confirm.html"
I also put the template folder in my project root like this:
myproject
accounts/
...
myproject/
templates/
The registration/ folder is now within this templates/ directory.
Together with the following settings everything works now, thank you for everyone that helped!
Settings:
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',
],
},
},
]
Django was looking for templates in my venv dir (my virtual environment dir) too, in my case, I solved it after I added my app in the settings file of the main project dir.
Basically in your app dir, there's an apps.py file which has a class named
*YOURAPPNAME*Config. In my case, the app is accounts so the class is AccountsConfig. Then go into your main project dir, which is located in same directory as your manage.py file, open settings.py, scroll down to INSTALLED_APPS and add your class there. For me it would be accounts.apps.AccountsConfig
Related
I'm using django-invitations to manage user invitation.
I can't find where I should put the template files email_invite_message.txt and email_invite_subject.txt
Here they talk about
Override email_invite_message.txt in django-invitations/invitations/templates/invitations/email/.
You can do this by creating the file in the same directory path in your project.
or
Yeah, if this isn't clear, you can create an .html file in your project at {projectroot}/{app}/templates/invitations/email/email_invite_message.html and it will override the default template.
But the first one didn't work for me and I can't figure out what {app} should be.
You can override any third-party application templates by creating replacement files in your project’s templates directory as indicated in the docs here.
In your settings.py file edit the TEMPLATES var, specifically the 'DIRS' key
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
...
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR + '/core/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',
],
},
},
]
let's suppose you have an app named core, edit the settings as in the previous example and put the new files in this way (for this particular app):
core/
/templates
/invitations
/email
email_invite_message.html
email_invite_subject.html
__init__.py
admin.py
apps.py
models.py
tests.py
I am trying to populate a html page with user's profile details on page load. Django document has this get_context_data() method in TemplateView, when I am using the same to display data to html page I get the following exception
TemplateDoesNotExist(', '.join(template_name_list), chain=chain)
django.template.exceptions.TemplateDoesNotExist: app/profile.html
Here is my code:
urls.py
url(r'^profile/', views.ProfileDetailView.as_view(), name='profile'),
views.py
class ProfileDetailView(TemplateView):
template_name = "appName/profile.html"
def get_context_data(self, **kwargs):
if 'user_name' in self.request.session:
username = self.request.session['user_name']
return render_to_response('appName:profile', {'username': username})
This is how am using in the html page
profile.html
<input type="text" class="form-control" placeholder="User name" id="usernameDisplay" name="usernameDisplay" readonly value="{{ username }}">
Where am I going wrong?
Edit1:
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',
],
},
},
]
P.S: All other templates are working fine the problem lies only with this one which I am trying to populate with data on page load.
Edit 2:
There was some naming issue the exception now is:
TemplateDoesNotExist(template_name, chain=chain)
django.template.exceptions.TemplateDoesNotExist: appName:profile
please check your TEMPLATES_DIRS properly
DIRS defines a list of directories where the engine should look for template source files, in search order.
in your settings.py
TEMPLATES = [
{
'DIRS': [os.path.join(BASE_DIR, 'templates')],
},
]
hope it helps
it seems your app looks for profile.html in app folder. So here's how the directory should look.
In your appName folder create a templates folder and the create a folder inside it with your "appName". I'm sure this folder is named app. Rectify it to the appName.
[projectname]/ <- project root
├── [projectname] <- Django root
│
├── appName/
| |
| |── templates/
| ├── appName
| └── Profile.html
|
│
├── manage.py
I would like to have a header/footer that follows throughout the entire site. Currently, I have a base.html file sitting within an app (project/app1/templates/app/base.html) and would like to apply an html template that I can use across the entire site. Is this possible or are templates only referanceable within the app templates folder? Can I have a template sitting in a higher directory such as project/template/base.html or project/base.html and call that so I don't have to copy and paste the same template into each app templates folder?
We can use a root "templates" directory for the project.
settings.py
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
# ... some options here ...
},
},
]
project structure
└── MY_PROJECT
└── BASE_DIR
├── my_proj
│ └── settings.py
├── manage.py
└── templates
For more information Read: https://learnbatta.com/blog/understanding-django-project-structure-26/
I am newly learning Django and was following the Learn Django 1.11 Tutorial.
Here is my current project tree:
├── manage.py
├── muypicky
│ ├── __init__.py
│ ├── old_settings.py
│ ├── settings
│ │ ├── base.py # Contains the settings (like shown in the tutorial)
│ │ ├── __init__.py
│ ├── urls.py
│ └── wsgi.py
├── requirements.txt
├── restaurants
└── templates # Templates Folder
└── index.html
I am trying to add the path to the tempelates folder in the settings folder. But the error shown
django.template.loaders.filesystem.Loader: .../muypicky/templates/index.html (Source does not exist)
Current setting.py file
TEMPLATES = [{
'DIRS': [os.path.join(BASE_DIR, 'templates')],
},]
Looking at the error, the file path is wrong because it goes into /muypicky/tempelates which is incorrect. So how do I get to root folder and then into tempelates folder with the given file tree in setting.py (base.py).
Any further queries, just ask and many thanks in anticipation.
Solution:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
and
TEMPLATES = [{'DIRS': [
os.path.join(BASE_DIR, 'templates')
],},]
Addition of os.path.dirname() was wrapped to go back one folder
settings.py
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
MEDIA_ROOT = os.path.join(BASE_DIR, 'uploads')
MEDIA_URL = '/uploads/'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [MEDIA_ROOT + '/'],#other app templates
'APP_DIRS': True,#heremention yourapp/templates
'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',
'django.template.context_processors.media'
],
},
},
]
also can you look at document djano template
I faced similar problem and was able to rectify with this:
TEMPLATES = [{
....
'DIRS': [os.path.join(BASE_DIR, '/templates')],
....
Dev Environment: django 2.2, python 3.6
so i think you need to add / before the 'templates' string...
I also think this changes with django versions as i'm currently working on another project that didn't require the trailing / before the 'templates' string on django 2.1, python 3.6. So i think this is worth trying out good luck
I am following part 2 of the Django tutorial. I am trying to override an admin template (base_site.html)
I copied the file from the django/contrib/admin/templates to mytemplates/admin/base_site.html
I also updated settings.py:
#Base Directory
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
#Template directories
TEMPLATE_DIRS = (os.path.join(BASE_DIR, 'mytemplates'),)
I tried putting the mytemplates folder in the root of the project folder as well as in the mysite folder with no luck. Any pointers would be great!
EDITED PREVIOUS USER RESPONSE -- THIS WORKS:
I think your relative path to the templates directory is wrong.
If you follow these steps it should work: (I tested it myself)
Put the mytemplates dir side by side with the manage.py file
project
-app1
-app2
-mytemplates
-admin
-base_site.html
-manage.py
Change the TEMPLATE_DIRS to:
TEMPLATE_DIRS = (os.path.join(BASE_DIR, 'mytemplates'),)
Make sure the order of the template loader is:
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)
#YardenST's answer almost worked for me. I guess it's a matter of configuration.
In case you run into trouble, I suggest you use this line:
TEMPLATE_DIRS = (os.path.join(BASE_DIR, 'mytemplates'),)
Next, put a breakpoint to show the actual outcome, or alternatively use print TEMPLATE_DIRS.
That's where you should place the templates you want to override.
#kat-russo, thx ;)
I tried to setup admin templates according to docs
project_name
-app1
-app2
-project_name //main folder -> settings.py , urls.py, wsgi.py
-templates
-admin
-project_name
base.html
without success, but
-templates
-admin
base.html
works for me.
my config (Django 1.10.4 w/Django CMS 3.4.1)
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',
'sekizai.context_processors.sekizai',
'cms.context_processors.cms_settings',
],
},
},
]
You can override all templates.
Create an admin directory in templates and add the files.
The all files.
https://github.com/django/django/tree/master/django/contrib/admin/templates/admin