Here is my settings.py
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__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',
],
},
},
]
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
STATIC_URL = '/static/'
And here is my template base_site.html in templates/admin/accounts/ (accounts is my app name):
{% extends "admin/base.html" %}
{% block title %}School name{% endblock %}
{% block branding %}
<h1 id="site-name">{{ site_header|default:_('Django administration') }}</h1>
{% endblock %}
{% block nav-global %}{% endblock %}
But this template is not overriding existing templates
In order to override the template templates/admin/base_site.html you have to have the same folder structure in your app.
You have myapp/templates/admin/accounts/base_site.html, but what you need is myapp/templates/admin/base_site.html. Then it should work.
The order of INSTALLED_APPS might be also important then.
-> Docs
Related
for login.html this is what I have
{% extends "./base.html" %}
{% block content %}
<h2>Tryin to Log In</h2>
{% endblock content %}
for base.html this is what I have
{% block content %}
<h2>base template</h2>
{% endblock %}
My problem is that the base.html overwrites the templates in login.html
I have looked into the settings.py and this is what I have
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',
],
},
},
]
The Answer is That include inserts a template into the current one, and extend will declare the template given as an argument as the current template's parent
I get a mistake when tying to add templates to my Django project.
settings.py :
TEMPLATE_DIRS = (
os.path.join(os.path.dirname(__file__), 'templates'),
)
views.py:
def index(request):
a = models.User.objects.get()
return render(request, "main/list.html", {"list": a})
and list.html:
{% extends "base.html" %}
{% block title %}Chats{% endblock %}
{% block content %}
{% if list%}
{% for i in list%}
<h2>{{i.usrname}}</h2>
{% endif %}
The project folder itself looks like this:
Main folder is app.
Thanks for your help!
You should include the os.path.join(...) in the Template 'DIRS'
Example:
In settings.py
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__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',
],
},
},
]
Then in your folder structure, you should have
app_name (main app folder) --> templates --> app_name(again) --> templates go here.
EDIT: I forgot to mention that in order to do this you must import os
treid change TEMPLATE_DIRS completly with this
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',
],
},
},
]
i tried to make a template tag to get the logged in user request.user , i tried this
in the settings.py
'context_processors': [
'django.core.context_processors.request',
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
and this is my template tag
from django import template
register = template.Library()
#register.simple_tag
def is_member(context):
request = context['request'].user
if request.user in request.user.model_name.admins.all:
return True
else:
return False
i have to make sure if the logged in user is one of members in admins (M2M) field
but i get this error
No module named 'django.core.context_processors'
and while i try to remove this line 'django.core.context_processors.request' in the settings.py file i get this error
'is_member' did not receive value(s) for the argument(s): 'context'
Updated - template
{% load my_tags %}
{% if is_member %}
<a class="nav-link" href="{% url 'listings:new_post' %}">
<i class="fas fa-user-plus"></i>create new post </a>
{% else %}
<a class="nav-link" href="{% url 'login'%}
<i class="fas fa-user-plus"></i>Login</a>
{% endif %}
any recommendation i will appreciate
thanks , art
Django documentation says:
django.core.context_processors
Built-in template context processors have been moved to django.template.context_processors
Now your template option should look like this:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'templates'),
],
'APP_DIRS': True,
'OPTIONS': {
'debug': DEBUG,
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
Also to use context in simple tags add takes_context=True
#register.simple_tag(takes_context=True, format_string)
def is_member(context):
....
And in your template:
{% is_member as user_is_member %}
{% if user_is_member %}
...
{% else %}
...
{% endif %}
I already saw all the links on stackoverflow related to this.
I am using Django 1.9.7 an I try to see in template if theuser is authenticated but I can't get the user.
Template code which is not printing anything:
{{ user.is_authenticate }}
{{ request.user.is_authenticate }}
Settings code:
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',
'social.apps.django_app.context_processors.backends',
'social.apps.django_app.context_processors.login_redirect',
],
},
},
]
I already tried withou social.apps and still not working, any ideas?
Make sure you pass the request context in the view, it's what adds the user to the context...
from django.template import RequestContext
def some_view(request):
form = None
# request.user accesses user here
return render_to_response('some_template.html', {'form':form}, context_instance=RequestContext(request))
Now in the template you can use:
{% if request.user.is_authenticated %}{{ request.user.email }}{% endif %}
I put this line in my urls.py site:
admin.site.site_header = 'My Site <img src="/static/LOGO.jpg" alt="logo" style="width:10%">'
The img tag displays on the page. Which I assume means the string is being HTML escaped.
Is there a command or setting to let the img tag work?
It would be disappointing if one still had to edit the template files to accomplish this.
Thanks in advance!
People how are looking for the solution they can try this. I have tried this in version 1.8 Django above.
Override the admin base_site.html template
{% extends "admin/base.html" %}
{% block title %}{{ title }} | {{ site_title|default:_('Django site admin') }}{% endblock %}
{% block branding %}
<img src="{{site_header}}"/>
{% endblock %}
{% block nav-global %}{% endblock %}
Save base_site.html in templates->admin folder if not create the folder.
Update TEMPLATES setting in your settings.py 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',
],
},
},
]
# Image to put at the top of the admin index page
admin.site.site_header = '/static/img/logo.png'