Can I modify the static_url setting in settings.py to another value, such as "/assets", without causing any issues?
You can change that value to whatever you'd like, without any serious issues, provided you are correctly using the {% static %} template tag to resolve URL paths to static assets.
Related
I know there are already several threads on the topic. I've been through most of them (especially all the troubleshooting listed in this one) but I can't figure out my issue.
I am trying to use a Bootstrap template in my Django project, and I'd like to simply start by accessing the files in the /static/ directory. My project directory looks like this :
Whenever I try to load the page http://localhost:8000/static/theme/assets/css/style.css it returns a Page not found error (and obviously no CSS/JS content appears on my index).
Here are my settings:
I have debug = True
ÌNSTALLED_APPS contains django.contrib.staticfiles
settings.py looks like this :
STATIC_URL = "/static/"
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static/"),)
But I still can't access anything from the /static/ directory.
I tried to access the CSS and JS files in base.html this way :
{% load static %}
...
<link href="{% static 'theme/assets/css/style.css' %}" rel="stylesheet">
I really have no clue how I could solve this.
Thanks in advance for your help !
Is base.html properly bound to a URL and to a view function via a URL dispatcher ? Can you access that file from your browser ?
If yes, try to substitute this line
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static/"),)
with this one
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),)
An already developed front end is making calls to the same static data files using different URLs
GET /static/images/1.png
and also using sometimes this:
GET /SomeDirName/static/images/1.png
Setting my STATIC_URL to "/static/" serve only the requests similar to the first example.
Is it possible to redirect the GET from the second example? Or how can I handle it?
Please note that Im not asking how to set STATICFILES_DIRS.
Instead of implementing a redirection mechanism, I strongly advise to solve the problem "upstream". The reason it aims to fetch the files with somedirname/static/images/1.png is because you do not use a leading slash in your URL. So you wrote something like:
<img src="static/images/1.png">
instead of:
<img src="/static/images/1.png">
It is however strongly advisable to use the {% static … %} template tag [Django-doc] and thus implement this as:
{% load static %}
<img src="{% static 'images/1.png' %}">
This will automatically generate the correct URL. If you later change the STATIC_URL setting [Django-doc], it will prepend the other STATIC_URL to the path, and thus make the use of static files more flexible.
In my dev. environment there is a /static/ folder, which atm. stores some images for the web-site.
My INSTALLED_APPS variable in settings.py does contain the
django.contrib.staticfiles app and
STATIC_URL = '/static/'
STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'static'),)
My urlpatterns variable in urls.py has this
urlpatterns+= static(settings.STATIC_URL,
document_root=settings.STATIC_ROOT)
Finally, in my template.html file i'm iterating through my cardset objects and am trying to provide an image for each object like this
<img src=" {{STATIC_URL}}/images/{{cardset.image}}" alt="{{ cardset.name }}" class="bn"/>
Now, the problem is:
a.) the {{STATIC_URL}} resolves as an empty string.
b.) i think i cannot use the static tag here because the variable {{cardset.image}} would not work well inside of the django tamplate {% ... %} with the static tag.
Could you please advice on what I should try doing here?
It seems to me you can still use the static tag. To build your string without using an image field, your link just needs tweaking slightly:
<img src="{% static "images/" %}{{cardset.image}}" alt="{{ cardset.name }}" class="bn"/>
which should render in html correctly. Depending what your {{cardset.image}} looks like you may be able to get rid of the images/ part in the middle.
Remember you also need to include {% load staticfiles %} at the top of your template.
Since cardset.image is a string, your assumption b is false; tags work perfectly well with variables:
{% static cardset.image %}
(The reason {{ STATIC_URL }} wasn't working was presumably because you haven't passed it to the template; it's not present automatically, it's just a standard template variable.)
So I've been hitting my head against the wall on this for the last hour and can't seem to figure out why none of the static media (CSS, Images, JS etc) when my template is rendered.
Could someone please help me find out why adjustments I need to make? Below are snippets from Settings.py, Index.html and stylesheet please let me know if more is needed.
My static files are located in the following directory:
/djangoproject/website/static
Settings.py - Located /djangoproject/djangoprojectname/
STATIC_ROOT = os.path.normpath(os.path.join(PROJECT_ROOT,
"/static/"))
STATIC_URL = '../website/static/'
Here's a snippet from my index.html that is supposed to be calling the css style sheet with {{ STATIC_URL }}
Index.html - Location /djangoproject/website/templates/
<link rel="stylesheet" href="{{ STATIC_URL }}css/style.css">
Location of CSS StyleSheet
style.css - Location /djangoproject/website/static/css/
From the Django docs:
If {{ STATIC_URL }} isn't working in your template, you're probably
not using RequestContext when rendering the template.
As a brief refresher, context processors add variables into the
contexts of every template. However, context processors require that
you use RequestContext when rendering templates. This happens
automatically if you're using a generic view, but in views written by
hand you'll need to explicitly use RequestContext To see how that
works, and to read more details, check out Subclassing Context:RequestContext.
It seems to me that you are setting STATIC_URL to a path, when it should be set to, well, a URL. You need to set this to the web address of the folder that contains your css files, for example:
STATIC_URL = 'http://mydomain.com/static_files/'
Try to find your CSS file online by typing the address you expect it to be into your browser. Once you find the CSS file this way, just copy the root URL that got you there.
I'm making a simple Django project but I cannot get any images to display in my pages.
Django documentation at https://docs.djangoproject.com/en/1.3/howto/static-files/#basic-usage states
Basic usage Put your static files somewhere that staticfiles will find
them.
By default, this means within static/ subdirectories of apps in your
INSTALLED_APPS.
Your project will probably also have static assets that aren’t tied to
a particular app. The STATICFILES_DIRS setting is a tuple of
filesystem directories to check when loading static files. It’s a
search path that is by default empty. See the STATICFILES_DIRS docs
how to extend this list of additional paths.
Additionally, see the documentation for the STATICFILES_FINDERS
setting for details on how staticfiles finds your files.
Make sure that django.contrib.staticfiles is included in your
INSTALLED_APPS.
For local development, if you are using runserver or adding
staticfiles_urlpatterns to your URLconf, you’re done with the setup –
your static files will automatically be served at the default (for
newly created projects) STATIC_URL of /static/.
You’ll probably need to refer to these files in your templates. The
easiest method is to use the included context processor which allows
template code like:
See Referring to
static files in templates for more details, including an alternate
method using a template tag.
So I did this in settings.py:
STATICFILES_DIRS = (
'/home/abc/django/project1/media/',
)
and enabled 'django.contrib.staticfiles',
In media, I have a folder img, which has various jpg files.
In my template, I have this as one of the lines:
<img src="{{STATIC_URL}}img/{{var}}.jpg">
When I'm passing var to this template via my view.
The HTML page seems to render this tag as "<img src="img/abc.jpg"> where var="abc".
But my browser refuses to display the image. What have I done wrong?
Did you see ths part in the documentation:
If {{ STATIC_URL }} isn't working in your template, you're probably not using RequestContext when rendering the template.
Do you also use RequestContext in your view to render the template?
Here is an alternative approach:
settings.py:
import os
PROJECT_DIR = os.path.dirname(__file__) + '/../'
STATIC_ROOT = os.path.join(PROJECT_DIR, 'media/')
STATIC_URL = '/media/'
In your template:
{% load staticfiles %}
<img src="{% static img/foo.jpg %}" />
If you need to pass a variable, use the prefix method:
{% load staticfiles %}
<img src="{% get_static_prefix %}img/{{var}}.jpg" />