http://docs.djangoproject.com/en/dev/howto/static-files/
This suggests that I can use STATIC_URL in my template to get the value from settings.py.
Template looks like this:
<link href="{{STATIC_URL}}stylesheets/tabs.css" rel="stylesheet" type="text/css" media="screen" />
Settings.py looks like this:
STATIC_ROOT = ''
STATIC_URL = '/static/'
When I go to the page I just get <link href="stylesheets/tabs.css" i.e. no STATIC_URL.
What am I missing?
You have to use context_instance=RequestContext(request) in your render_to_response, for example:
return render_to_response('my_template.html',
my_data_dictionary,
context_instance=RequestContext(request))
Or use the new shortcut render
As Dave pointed out, you should check if django.core.context_processors.static is in your TEMPLATE_CONTEXT_PROCESSORS variable in settings.py. As the docs said, it`s there by default.
It is not recommended to directly use the STATIC_URL variable. See the accepted answer in this question
Instead of
{{STATIC_URL}}stylesheets/tabs.css
use
{% load staticfiles %}
{% static 'stylesheets/tabs.css' %}
I have the same problem, solved like this:
in settings.py
add:
django.template.context_processors.static
here:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': TEMPLATE_DIRS,
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.template.context_processors.static',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
Related
So far, on my entirely blank django project I have created a simple template that looks as following.
test.html:
{% block includes %}
{{ mytext }}
{% endblock includes %}
to my views.py I added the following function:
from django.template.response import TemplateResponse
def testdex(requset, template_name="test.html"):
args = {}
text = "hello world"
args['mytext'] = text
return TemplateResponse(request, template_name, args)
My settings.py includes the following:
TEMPLATE_DIR = os.path.join(BASE_DIR, "core/templates")
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATE_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',
],
},
},
]
However, loading the template only shows me a blank page. Is there something I am missing?
I'm getting a broken image in a django project, but as far as I have checked, the code looks right.
Here's the code in settings.py:
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
I have also imported the media option in the templates section:
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.template.context_processors.media',
],
},
},
]
Here's the code I'm using in the template.
{% if request.user.profile.picture %}
<img src="{{ request.user.profile.picture.url }}" height="35" class="d-inline-block align-top rounded-circle">
{% else %}
<img src="{% static 'img/default-profile.png' %}" height="35" class="d-inline-block align-top rounded-circle">
{% endif %}
The default-profile.png image is loading correctly, but the other profile pictures are not, although they have been loading correctly to the media folder and their urls are displayed correctly in the database. Anyone knows what I'm doing wrong?
EDIT:
The HTML output of the image is the following:
<img src="/media/users/pictures/Foto.jpg" height="35" class="d-inline-block align-top rounded-circle">
It looks correct when checking the src tag, as it matches the MEDIA_URL with the valur that was stored in the database after it.
What appears to be happening is that you're not serving the media files correctly during development. You can try appending this to the urlpatterns list on your site's urls.py file.
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Please refer to this information in the docs for more details.
I need to incorporate css in my templates to help them look better, but inspite of adding the static url and root, I am just not able to load it in my template. I am attaching the relevant code here. Please tell me what am I doing wrong. Thanks in advance.
settings.py
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR,'templates')
STATIC_DIR = os.path.join(BASE_DIR,'static')
STATIC_URL = '/static/'
STATIC_ROOT = [STATIC_DIR,],
index.html
<!DOCTYPE html>
{% load staticfiles %}
<html lang="en">
<head>
<link href="{% static 'css/index.css' %}">
</head>
settings.py
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'project_name/static')
]
index.html
<!DOCTYPE html>
{% load static %}
<html lang="en">
<head>
<link href="{% static 'css/index.css' %}" rel="stylesheet">
</head>
for more information check here
for loading static files you need to add the static url too
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
......
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
add these in your project's root url
Change url base urls.py like following
urlpatterns = [
# your url here
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Also include {% load static %} tag in your template
There is no settings named STATIC_DIR, it should be STATICFILES_DIRS and should be declared like this:
STATICFILES_DIRS = [
os.path.join(BASE_DIR,'static'),
]
STATIC_ROOT = os.path.join(BASE_DIR,'staticroot') # Static Dir and Static root needs to be different
As for serving static files, as per documentation, if you add django.contrib.staticfiles in INSTALLED_APPS, then django will serve static automatically when DEBUG is True. But in production, you need to use a reverse proxy server to static files. Or you can use whitenoise. More information can be found in documentation as well.
Which version of Django do you using? Starting from 2.0 there are some changes, instead of
TEMPLATE_DIR = os.path.join(BASE_DIR,'templates')
there is
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',
],
},
},]
Also check out is your static folder even exist
And be sure that you have chosen right path to your static folder(for example I hold this folder inside of template folder so my code looks like this)
STATIC_URL = '/templates/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "templates/static"),]
And I see that you didn't put csrf_token to your html, so please add this to your html
{% csrf_token %}
I've built a menu as a templatetag in Django>1.9.
The problem is that following this solution, I can't put the templatetag at the root of the folder, as I'm getting a:
TemplateSyntaxError: 'menu' is not a registered tag library
Below is the part of my settings.py that I've modified:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
# Look for base template at root of project
'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',
],
# Look for base templatetags at root of project
'libraries': {
'project_tags': 'templatetags.menu',
}
},
},
]
Even with an empty menu.py template tag like the one below I get the same error:
from django import template
register = template.Library()
#register.inclusion_tag('menu.html', takes_context=True)
def menu(context):
pass
Does Django support project-wide templatetags at all?
I've had it fixed by loading in my base.html template the module (i.e. project_tags) instead of the tag (i.e. menu), and then calling normally the tag menu:
So it's
{% load project_tags %}
...
{% menu %}
Instead of:
{% load menu %}
...
{% menu %}
I'm starting a very simple Django app but having trouble with extending an html file.
I have base.html and index.html both within my_site/my_app/templates/my_app.
i.e.
my_site/my_app/templates/my_app/base.html and my_site/my_app/templates/my_app/index.html.
Within the index.html file I have {% extends 'base.html' %}.
My settings.py file has
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',
],
},
},
]
But when I visit my index view at http://127.0.0.1:8000/index/:
def index(request):
return render(request, 'my_app/index.html')
I get the following error:
TemplateDoesNotExist at /index/
base.html
Request Method: GET
Request URL: http://127.0.0.1:8000/index/
Django Version: 1.10.3
Exception Type: TemplateDoesNotExist
Exception Value: base.html
Do I have the base.html file saved in the wrong place or is it something else? I have not been able to solve this.
It should be in my_site/my_app/templates or my_site/templates.