Django 1.4 serving media files works, but not static - python

I am using Django 1.4 and for some reason i am able to serve media files, but not the static ones...
Here is my code:
settings:
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
STATIC_URL = '/static/'
ADMIN_MEDIA_PREFIX = '/static/admin/'
urls.py:
(r'^media/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT}),
(r'^static/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.STATIC_ROOT}),
base.html:
<link href="{{ STATIC_URL }}css/bootstrap.css" rel="stylesheet">
<link href="{{ MEDIA_URL }}css/bootstrap-colorpicker.css" rel="stylesheet">
i get a 404 http not found... what am i doing wrong? An i did create the static folder in my project right next to media
http://mysite.com:8000/static/css/bootstrap.css

Your static folder should be under one app that you use it for.
For example, I have a project named my_project and an application named my_app, I have some static files used in my_app so I put them under ~/project_path/my_project/my_app/static
NB: my_app must be in INSTALLED_APPS. See STATICFILES_FINDERS documentation.
Edit:
As a best practice, you should have a global static folder in one app (the main one), for example a static folder how contains your html template basic resources as jquery, bootstrap, your global style.
And for the static files how's required only for one app, for example app foo, these files should be under foo/static folder

I suggest removing the explicit media and static views and allowing the staticfiles app to create them (when DEBUG is True under development).
Check the default finders are present in your settings.py
https://docs.djangoproject.com/en/1.4/ref/contrib/staticfiles/#std:setting-STATICFILES_FINDERS
Either add your project static directory to STATICFILES_DIRS (https://docs.djangoproject.com/en/1.4/ref/contrib/staticfiles/#std:setting-STATICFILES_DIRS) or place app specific static folders under each app. The app needs to be listed in the INSTALLED_APPS for the finders to locate the static content.
Do not place static files into STATIC_ROOT yourself. This directory is managed by the collectstatic command. See https://docs.djangoproject.com/en/dev/howto/static-files/#deployment

Here is how i define my media url in settings.py:
import os
ABSOLUTE_PATH = os.path.dirname(__file__)
MEDIA_ROOT = os.path.join(ABSOLUTE_PATH, '../media')
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(ABSOLUTE_PATH, '../static')
STATIC_URL = "/static/"
So like you see the difference is ../media and ../static
Is mysite in your installed apps ? Django look inside your installed apps and check if there's a static folder there.

Related

img not showing in Django static

I use Django 2.0.3 and I want try static , I create one in app folder and my code like this.
{% load static %}
And for pic is :
<img src="{% static 'images/ic_team.png' %}">
in setting the dir of static is :
STATIC_URL = '/static/'
but the pic comes like this :
enter image description here
you have to actually route /static to serve your static files ...
normally you would do this in production using apache or nginx or simillar
if you are running with debug=True in your settings you can use django static file finders (something like the below)
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]
STATICFILES_DIRS = [ os.path.abspath(os.path.join(BASE_DIR,"..","usr","local","www","documents","media")),
]
but be aware it wont work unless debug is set to true, and this is never recommended for production servers
Make sure that your static folder is inside the app folder in your case will be something like:
your_app_name/static/images/ic_team.png
https://docs.djangoproject.com/en/2.0/howto/static-files/
You have this in the settings.py
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
put your static files in the project root folder

Receiving the 404 error in getting of static django files

Please help to understand what I do wrong.
I have in my settings.py :
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
STATIC_URL = os.path.join(PROJECT_ROOT, 'static').replace('\\','')+'/'
And in index.html :
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static "/css/table.css" %}">
But I still have the error 404 :
"GET /var/cardsite/cardsite/static/css/table.css HTTP/1.1" 404 1696
I have this file :
ls -la /var/cardsite/cardsite/static/css/table.css
-rw-r--r-- 1 root root 77 Sep 25 16:15 /var/cardsite/cardsite/static/css/table.css
So what is going on?
P.S. My project stored on "/var/cardsite" and I want to make static folder on each application, like in example is default application "cardsite"
thanks
Read this Django_Docs
You must also have a STATIC_ROOT option set before you can use the static files, heres some help
add this to your code:
STATIC_URL = os.path.join(PROJECT_ROOT, 'static').replace('\\','')+'/'
# Here you can add all the directories from where you want to use your js, css etc
STATICFILES_DIRS = [
# This can be same as the static url
os.path.join(PROJECT_ROOT, "static"),
# also any other dir you wanna use
"/any/other/static/path/you/wanna/use",
]
# This is the static root dir from where django uses the files from.
STATIC_ROOT = os.path.join(PROJECT_ROOT, "static_root")
you will also need to specify it in the urls.py file, just add the following code to the urls.py file:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
after you add this, run the command:
python manage.py collectstatic
This will copy all the statics you need to the static root dir.

Django: Where to store global static files and templates?

So I am working on a Django (1.9) project, and have the need to use the same templates and static files across multiple apps. I know how to use templates and static files stored outside each app (using STATICFILES_DIRS and TEMPLATEFILES_DIRS in settings.py), but where should I store them?
My guess would be in folders in the project directory.
home/user/myproject/staticfiles
home/user/myproject/templates
But if there are official reconsiderations (which I have been unable to find in the documentation), I would prefer to comply with those.
Also, if there are any other recommendations for using templates and static files outside of the normal django app directories, that you think I should know about, I'd really appreciate you telling me (or telling me where to look).
Cheers :)
Assume there is a global css file located here:
home/user/myproject/staticfiles/css/global.css
Change settings.py to match this:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "staticfiles"),
]
Inside of a template, for example, index.html:
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'css/global.css' %}" />
Now Django can find global.css.
in your settings.py file add the following
STATIC_URL = '/static/' # this will be added before your file name
STATIC_ROOT = 'path/to/your/files/' # this will help django to locate the files
read more about it here: https://docs.djangoproject.com/en/1.9/howto/static-files/
While nu everest's answer is correct and working (after you restart the server), in my case it lacks the advantage of my IDE autosuggesting the static files' paths. So I went for specifying a relative path inside my project. For simplicity I named the directory 'static' (same as the STATIC_URL variable) but of course any name is possible.
settings.py:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
"myproject" + STATIC_URL
]
What's important here: The root is actually not the settings.py's parent, but its parents's parent, as in:
BASE_DIR = Path(__file__).resolve().parent.parent

How to add my own files to django 'static' folder

I've read django static files document and made my django static files settings like this
setting.py
PROJECT_PATH = os.path.realpath(os.path.dirname(__file__))
STATIC_ROOT = os.path.join(PROJECT_PATH, 'static')
STATIC_URL = '/static/'
html page
<img src="{% static "admin/img/author.jpg" %}" alt="My image"/>
So if I address one of django default static files, it works fine. But if I add my own file and folders to the static folder, it doesn't show it.
I tried
python manage.py collectstatic
But nothing changed. How can I make it work?
A few things...
STATICFILES_DIRS = (
'path/to/files/in/development',
)
STATIC_ROOT = 'path/where/static/files/are/collected/in/production'
When DEBUG = True, Django will automatically serve files located in any directories within STATICFILES_DIRS when you use the {% static 'path/to/file' %} template tag.
When DEBUG = False, Django will not serve any files automatically, and you are expected to serve those files using Apache, Nginx, etc, from the location specified in STATIC_ROOT.
When you run $ manage.py collectstatic, Django will copy any and all files located in STATICFILES_DIRS and also files within any directory named 'static' in 3rd party apps, into the location specified by STATIC_ROOT.
I typically structure my project root as such:
my_project/
/static_assets/ (specified in STATICFILES_DIRS)
/js
/images
/static (specified in STATIC_ROOT)
I hope that helps you understand how the staticfiles app works.
STATIC_ROOT is where files are collected and moved to when collectstatic runs.
If you want files to be consolidated to there they should be found by the static file finder.
As an example, include the following in your settings
STATICFILES_FINDERS = ("django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder")
now for one of the apps that are a part of your project, create a folder called static and put something in it.
When you run collectstatic you should see that file mentioned and it copied to your STATIC_ROOT
You can try adding
STATICFILES_DIRS = (
STATIC_PATH,
)
to your settings.py.
Also, if you haven't already, make sure to include
{% load static from staticfiles %}
in each of your templates where you wish to reference static files.
Lastly, make sure that the file you are referencing actually exists and the file path to it is correct.
Try to:
<img src="{{ STATIC_URL }}admin/img/author.jpg" alt="My image"/>
And in your view must be something like this:
...
from django.shortcuts import render_to_response
from django.conf import settings
def my_view(request):
...
static_url = getattr(settings, 'STATIC_URL')
...
return render_to_response(
template_name,
RequestContext(request, {
'STATIC_URL': static_url,
}))

How to work properly with django static files

i have a problem with django static files handling.
I am confused about how to use MEDIA_ROOT, STATIC_ROOT, MEDIA_URL and STATIC_URL
I have a file structure like this(sorry, i dont know how to indent properly :S):
static/
css/
img/
js/
For example, if I have a .css file in my css dir, how could I reach it?
MEDIA_ROOT and MEDIA_URL are deprecated. Use STATIC_ROOT and STATIC_URL.
STATIC_ROOT = "Absolute path to your static directory" (Example: /home/your_app/static)
STATIC_URL = "/static/" (Could be anything you would like to use)
When you want to serve static content pass the context_instance and in your template use,
{{STATIC_URL}}/file_path_from_static_directory/
EDIT:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()

Categories