I am working with Django 1.8.4 (latest).
I’m stuck with the official Django tutorial steps from: https://docs.djangoproject.com/en/1.8/intro/tutorial02/#customize-the-admin-look-and-feel
I am trying to rename “Django administration" but nothing happens.
I searched for the answer and found the totally same question here: _http://stackoverflow.com/questions/28787823/cant-change-django-admin-template There is now answer.
I did exactly the same as described there and in the tutorial.
As you can see from my repo: https://github.com/legobillyjoe/django-tutorial
the location of my base_site.html is mysite/templates/admin, as suggested in the tutorial.
I have added in settings TEMPLATES = [os.path.join(BASE_DIR, 'templates')], but nothing changes.
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',
],
},
},
]
Any suggestions what I can do in order to make it work?
Did you run the makemessages command after you changed the translation strings? Also the default will only appear if the variables return None types
But actually, there's no need to override any templates for your requirement, in your admin file after your import statements add the following:
admin.site.site_header = _(u"Title")
admin.site.index_title = _(u"Subtitle")
So you have something like
from django.contrib import admin
from django.utils.translation import ugettext as _
admin.site.site_header = _(u"Title")
admin.site.index_title = _(u"Subtitle")
class FooAdmin(admin.ModelAdmin):
etc...
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'm using django 3.0.
I use django-admin startproject mysite and created a sample django project.
I don't think the sample project has any models so I commented out "django.contrib.contenttypes" in INSTALLED_APPS in settings.py. I also commented out all middlewares.
I then wrote a simple view
from django.shortcuts import render
def index(request):
return render(request, 'hello.html')
and hello.html is just a blank file.
Once I access the page, django throws exception
Model class django.contrib.contenttypes.models.ContentType doesn't
declare an explicit app_label and isn't in an application in
INSTALLED_APPS.
Can anyone help explain the exception? The sample project doesn't have any model, why do I need django.contrib.contenttypes? Can django websites live without django.contrib.contenttypes?
I have to remove django.contrib.auth.context_processors.auth in TEMPLATES.
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [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',
],
},
},
]
What is the standard, preferred way to make settings available to templates in Django 1.8? Currently, I define a custom context processor in my project and then reference it in my TEMPLATES OPTIONS setting. I've looked through the docs but didn't find this issue mentioned. This previous Stackoverflow question says to do what I'm doing but it's over two years old and I'm wondering if there's a newer preferred method. If I've learned one thing from recently upgrading Django, it's to do things the way the framework wants you to do them. You'll have fewer problems this way.
Thanks.
# utils/context_processors.py
from profile.models import UserProxy
from conf.settings import base as base_settings
def global_constants(request):
"""Constants that are available to all templates."""
return {
'site_name': base_settings.SITE_NAME,
'media_url': base_settings.MEDIA_URL
}
# myproject/settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug', # default
'django.template.context_processors.request', # default
'django.contrib.auth.context_processors.auth', # default
'django.contrib.messages.context_processors.messages', # default
'utils.context_processors.global_constants', # project
],
},
},
]
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.
I have a small typography related templatetag library that I use on almost every page. Right now I need to load it for each template using
{% load nbsp %}
Is there a way to load it "globally" for all views and templates at once? Putting the load tag into a base template doesn't work.
There is an add_to_builtins method in django.template.loader. Just pass it the name of your templatetags module (as a string).
from django.template.loader import add_to_builtins
add_to_builtins('myapp.templatetags.mytagslib')
Now mytagslib is available automatically in any template.
It will change with Django 1.9 release.
Since 1.9, correct approach will be configuring template tags and filters under builtins key of OPTIONS - see the example below:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'builtins': ['myapp.builtins'],
},
},
]
Details:
https://docs.djangoproject.com/en/dev/releases/1.9/#django-template-base-add-to-builtins-is-removed
In django 1.7 just replace for from django.template.base import add_to_builtins
In Django 1.9 there is an libraries dictionary of labels and dotted Python paths of template tag modules to register with the template engine. This can be used to add new libraries or provide alternate labels for existing ones.
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',
],
'libraries': { # Adding this section should work around the issue.
'custom_tags' : 'myapp.templatetags.custom_tags',#to add new tags module.
'i18n' : 'myapp.templatetags.custom_i18n', #to replace exsiting tags modile
},
},
},
]