My link looks like this:
<link rel="stylesheet" type="text/css" href="{% static 'css/background.css' %}">
This is fine because it gets the path I want which is href="/static/css/background.css". I use {% load static}. I have django.contrib.staticfiles in my INSTALLED_APPS. This is some more relevant info on my settings.py:
STATIC_URL = '/static/'
STATICFILES_DIR = [
os.path.join(BASE_DIR, "static"),
]
But I'm pretty sure this is right too. My problem is that static/css/background.css cannot be found by my local server. I know this because http://localhost:8000/static/css/background.css gives me a 404 error. I don't know why this is the case because this is my project file path:
Project
Project
src
static
css
background.css
Have you set your url paths ? Here is a snippet from the official Django documentation.
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
I fixed my problem and I'm posting this in case anyone this can help anyone else. My problem is that I had my static folder outside my app. It turns out that my website is targeting my app and so when django was looking for the static folder it was looking for it in my app. So try putting your static files in your app and see if that helps.
Related
Not sure what is going on, but my admin page on my Django site isn't loading any CSS. Not sure if it has to do with python manage.py collectstatic but ever since I served my static files using this, my admin page doesn't load with CSS and instead, it's plain HTML. This same issue is happening with a few projects of mine when using collectstatic. Any ideas? They'd be appreciated. Since I don't know what the issue is or where to start, I'm not sure what code to provide. If you want to see anything, let me know.
I've noticed when I viewed the source of the admin page and clicked on the css links (<link rel="stylesheet" type="text/css" href="/static/admin/css/base.css">), the base.css file seems to be corrupted maybe ? What the base.css looks like All the other css files when clicked are readable. What the other css files look like
Add the following code to the settings.py file
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static')
STATIC_URL = '/static/'
After that add the following in the URL:
static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
I want to make web site which show file tree in web and print image/video on web.
I'm planning to print it with html tag img and video. However there are some problems with path. Most of my static files( like video or img) outside of django project folder.
For example django template source and view source is at /Users/Knight/My-site/Damotorie_cafe/views.py and image is at /Applications/MAMP/practice
I tried to get relative path with python code and use it for src attribute.
Same path works well when I don't use django and just open it with local web browser, however when I open it with django then error occur.
I'm starter in django. Can any one help me?
(I searched it before asking, but most of question is about upload not show image.)
You need to configure your static files in Django. In the settings.py, you need to set up:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
'/path/to/your/images/outside/web/tree/'
]
STATIC_URL = '/static/'
In development, you need to set this in your base urls.py file:
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^login', auth_views.login, name='login'),
...
]+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Then in your template files, first set this at the top of the template file, so you load the 'static' tag:
{% load staticfiles %}
Then you use the static variable:
<img src="{% static 'path/img' %}"/>
The trick is, Django searches through STATICFILES_DIRS to find that 'path' in your template file, so the end 'path' has to be different from anything you have in your other listed STATICFILES_DIRS. It searches the list in order, and will find the first one that matches, in any event.
I'm currently mad at Django (1.9) right now! The saddest thing is 'Static URL' is the one giving me problem. 'Media URL' is working fine, no problem, but the static url is giving a huge headache.
in my settings_dev.py
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PROJECT_PATH = os.path.abspath(os.path.dirname(__file__))
STATIC_ROOT = os.path.join(PROJECT_PATH,'../static/')
STATIC_URL = '/static/'
when I add the below tag:
{% load static from staticfiles %}
<script type="text/javascript" src="{% static 'datepicker/js/bootstrap-datepicker.js' %}"></script>
The js file won't load. when I check my source code, it will display the below link.
<script type="text/javascript" src="/static/datepicker/js/bootstrap-datepicker.js"></script>
And when I click it will redirect me to
http://127.0.0.1:8000/static/datepicker/js/bootstrap-datepicker.js
And display
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/static/datepicker/js/bootstrap- datepicker.js
Now, I adjusted my urls.py to
if settings_dev.DEBUG:
# static files (images, css, javascript, etc.)
urlpatterns += patterns('',
(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings_dev.MEDIA_ROOT, 'show_indexes': True}),
(r'^static/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings_dev.STATIC_ROOT, 'show_indexes': True}),
)
Yet, I'm still getting the same error!! Page not found issues.
Project Directory
PROJECT NAME: Book/
SUB DIRECTORY:
media
static
Template
book
bookapp
manage.py (this is a file)
What am I missing?
Okay to make things clear for you.
STATIC_ROOT is that directory where all your static data gets collected when you want to serve your files on another server, say APACHE or NGINX or maybe on Heroku or so.
If don't want just want to run your web-app on your local development server, you don't require python manage.py collectstatic and hence you don't need STATIC_ROOT.
All you need is STATIC_URL and in case you have your static files at some other location as well then you also need STATICFILES_DIRS = [os.path.join(BASE_DIR, "static"),].
So you'll be having a folder named `static' at the base directory level where django will look for your static files.
If you specify the same folder for STATIC_DIRS and STATIC_ROOT then you'll automatically get an error. Django won't allow you to do that as technically you are trying to give the same directory for two different purposes.
See this for a detailed explanation -> Differences between STATICFILES_DIR, STATIC_ROOT and MEDIA_ROOT
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static"),]
That line is enough for serving your project static folder files... And you have to set this in your urls.py
urlpatterns = [
...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
So I have my Django project in a folder on my server, like so:
www.mydomain.com/myfolder/
The index page loads, but things like the css document don't because the paths are improper. Am I missing a shortcut? I looked through the settings.py documentation for a place to specify my site folder, but didn't find anything to help.
So example, if I specify this in my settings:
STATIC_URL = 'static/'
Then it will look for my css doc on my /myfolder/login page at:
http://mydomain.com/myfolder/login/static/stylesheet.css
If I specify:
STATIC_URL = '/static/'
Then it will look for my css doc on my /myfolder/login page at:
http://mydomain.com/static/stylesheet.css
Meanwhile, my css document is at:
http://mydomain.com/myfolder/static/stylesheet.css
Do I need to change everything and specify /myfolder/ before everything?
I think you need to add path of you STATIC_ROOT in settings, and update urls.py file.
My example:
#settings.py
....
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static",)
#path to your static folder
#urls.py
...
from django.conf import settings
from django.conf.urls.static import static
....
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
This question has a good practice solution on django tutorial:
See:
https://docs.djangoproject.com/en/1.6/intro/tutorial06/#customize-your-app-s-look-and-feel
That you should create a /static/appname/ folder inside each of your app, and then put the static files inside it.
So in your app, the static file url should turned into something like:
http://mydomain.com/static/app_name/stylesheet.css
Which won't collides with other app.
You'd better read the link I offered above, the tutorial shows a good habit.
Don't you just need to do this?
STATIC_URL = '/myfolder/static/'
i'm a Django newbie working on my first project and having a problem with static files.
I have created a simple auth system using django.contrib.auth consisting of two templates: mysite/templates/index.html and mysite/templates/registration/login.html. I have global static content in mysite/static which I want to be able to access on all templates rendered by all apps.
mysite/templates/index.html contains <img src="{{ STATIC_URL }}pics03.jpg"/> which renders as "static/pics03.jpg" and loads fine when I visit the url localhost:8000/
mysite/templates/registration/login.html contains <img src="{{ STATIC_URL }}pics03.jpg"/> which also renders as "static/pics03.jpg" and does not load when I visit the url "localhost:8000/accounts/login/"
In my urls.py I have:
urlpatterns = patterns('',
url(r'^$', 'mysite.views.home'), # plays index.html template
url(r'^accounts/login/$', 'django.contrib.auth.views.login'),
In my settings.py I have:
PROJECT_DIR = os.path.dirname(__file__)
STATICFILES_DIRS = (
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
os.path.join(PROJECT_DIR,'static'),
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
STATIC_URL = '/static/'
STATIC_ROOT = ''
I was under the impression that Django should be looking for global static content in STATICFILES_DIRS, but it doesn't find the static content for login.html even if I change the url in there to an absolute path to the static folder. Can anyone shed any light on this?
Your problem is that you arent listening to the URL "/static/" nowhere in your urls.py
If you serve your application via a webserver like apache or nginx then this is normal as the webserver would handle the static files itself.
For development Django comes with a built-in static server
to urls.py, at the very end add
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()
What this does is to add the /static/ url and let you serve those without a webserver.
This is equivalent to
url(
regex=r'^static/(?P<path>.*)$',
view='django.views.static.serve',
kwargs={'document_root': settings.STATIC_ROOT,}
)
some people will tell you that you need to wrap the URL-rules in a "if settings.DEBUG" to use the dev-only rules, but this isnt needed at all and actually i find that to be a bad advice.
Are you having trouble when using the build in runserver or are you serving using Apache or similar? I've struggled with this a bit. The documentation I follow is: https://docs.djangoproject.com/en/stable/howto/static-files/
The second part is key when you are ready to deploy. You need to define a static root (which will be empty to begin with) and run the manage.py collectstatic command to move the static files from throughout your project into that folder. Then you can serve them from there.
Does changing STATIC_ROOT='' to STATIC_ROOT='/' help?
It seems to me the only difference is that static/pics03.jpg (relative path) exists on the home page, but doesn't on the other.
The absolute path /static/pics03.jpg exists in both cases. If changing STATIC_ROOT doesn't help, just add a / to the beginning of the urls.