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
Related
How to define a directory for storing all templates of my app? I am having a hard time finding the explanation how to set templates directory in Django settings.py file.
To set your path to templates folder you need to set your DIRS key with the value in which you are giving your path to templates as shown in the example below where at first i am setting a variable TEMPLATES_DIR where BASE_DIR give me the path to the folder where my manage.py is being kept and i concatinate projects directory alongwith templates directory because in my case i kept it inside project directory.
For example :-
TEMPLATES_DIR = os.path.join(BASE_DIR, 'project','templates')
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATES_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',
],
},
},
]
So, Here's the explaination What i did is set the TEMPLATES_DIR variable with reference to base directory that will take the path to folder where manage.py of your django project is being kept now i am concatinating the projects and templates directory just to navigate the exact folder structure that you kept for your templates in your project.
I hope you found this helpful. if you have any more doubts i'll be happy to answer those also.
Happy Coding. :-)
The way to set your templates directory in Django is by setting the value of the 'DIRS' key in TEMPLATES inside the settings.py like so (assuming it is present just inside your base directory (BASE_DIR):
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',
],
},
},
]
In Django 3, the templates directory can be added to 'DIRS' like this as well:
'DIRS': [str(BASE_DIR.joinpath('templates'))]
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
What is the correct way to prepend an extra, site-specific templates directory to the template dirs setting in Django?
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
<PREPEND SITE-BASED TEMPLATE DIR IN HERE>
str(APPS_DIR.path('templates')),
],
'OPTIONS': {
'debug': DEBUG,
'loaders': [
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
],
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'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',
],
},
},
]
I obviously can't use Site.objects.get_current() in there.
I could write my own template loader which uses it but is that "correct"?
Or should I be using an extended version of the settings files or something?
First of all, start by separating the settings. You should have a settings hierarchy that goes like:
base_settings.py
siteA_settings.py
siteB_settings.py
On base_settings.py you should put all the settings that are project-wide.
siteA_settings.py (which is a child) will override the base_settings.
To make it site-specific you have to put on each setting file the SITE_ID setting. Example of siteA_settings.py
# Sites Framework
SITE_ID = 1
SITE_TITLE = 'First Site'
Then load the TEMPLATES settings. They can be different on each setting file. Example:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'templates'),
],
'APP_DIRS': True,
... , etc.
Just like TEMPLATES, you can choose to have different settings for each Site (apps, email settings etc)
Then, when you run the server, you should specify which setting (site) you want to run. Example:
python manage.py runserver --settings=myproject.siteA_settings
where myproject is the app where your settings are located.
Do not forget that you have to put the --settings argument everytime you run a command (including makemigrations, migrate, etc etc)
Usually, on my projects, I create a core app, a settings directory and inside it, I put base_settings.py, dev.py, local.py, production.py etc. When running the Django Development Server, I then run the command:
python manage.py runserver --settings=core.settings.local
I set an address for the DIRS in TEMPLATES setting in my project's settings.py file and also i change APP_DIRS to False.
now, all of the templates will running but when i go to localhost:8000/admin, it generates an error that said "Template Does Note Exists".
TemplateDoesNotExist at /admin/
admin/index.html
i dont want to APP_DIRS be True.
how can i solve this problem?
this is the settings
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'templates'),
],
'APP_DIRS': False,
'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',
],
},
},
]
If you dont want APPS_DIR to be TRUE, copy templates of admin app to your template directory. When you set APP_DIR to True django will check for a directory named templates in each apps and if found load specified templates from that directory. Else django will check for a directory with same name as of app in the global template directory if found render specified template.
In you case APP_DIR is set to False and there is no directory named as admin in global template folder. So copy template directory from django/contrib/admin/ of your virtual environment and place it in your global template directory. I suppose your global template directory is in the project directory itself.
os.path.join(BASE_DIR, 'templates'),
HTH :)
I'm going through the very basic tutorial here and I am using my own base_site.html template instead of the default one, which has a different header text.
I copied the default template into my template folder, modified it, then linked the new template in settings.py:
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': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
No dice.
When I went a much simpler route and instead simply linked it with the single line below, it worked easily:
TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates/')]
Which is great! But now I want to know why the original code doesn't work.
If it helps, my folder structure is:
- Scripts
- mysite
- mysite
- polls
- templates
- admin
base_site.html
The TEMPLATES setting will be introduced in the django 1.8. It is unavailable in the current django 1.7.
So read the tutorial for the actual version of django you using.