i 've the following folder structure
src\BAT\templates\admin\base.html
src\BAT\media\base.css
src\BAT\media\admin-media\base.css
settings.py
MEDIA_ROOT = os.path.join( APP_DIR, 'media' )
MEDIA_URL = '/media/'
ADMIN_MEDIA_PREFIX = '/admin-media/'
TEMPLATE_DIRS = (
os.path.join( APP_DIR, 'templates' )
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.admin',
'django.contrib.admindocs',
)
urls.py
urlpatterns = patterns('',
(r'^admin/doc/', include('django.contrib.admindocs.urls')),
(r'^admin/', include(admin.site.urls)),
(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
)
I need to get both the CSS files in my application. my base.html contains
<head>
<title>{% block title %}{% endblock %}</title>
<link href="{{ MEDIA_URL }}css/base.css" rel="stylesheet" type="text/css" />
<link href="{{ MEDIA_URL }}{{ADMIN_MEDIA_PREFIX}}css/base.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" href="{% block stylesheet %}{% load adminmedia %}{% admin_media_prefix %}css/base.css{% endblock %}" />
{% block extrastyle %}{% endblock %}
<!--[if lte IE 7]><link rel="stylesheet" type="text/css" href="{% block stylesheet_ie %}{% load adminmedia %}{% admin_media_prefix %}css/ie.css{% endblock %}" /><![endif]-->
{% if LANGUAGE_BIDI %}<link rel="stylesheet" type="text/css" href="{% block stylesheet_rtl %}{% admin_media_prefix %}css/rtl.css{% endblock %}" />{% endif %}
<script type="text/javascript">window.__admin_media_prefix__ = "{% filter escapejs %}{% admin_media_prefix %}{% endfilter %}";</script>
{% block extrahead %}{% endblock %}
{% block blockbots %}<meta name="robots" content="NONE,NOARCHIVE" />{% endblock %}
</head>
I want to get the following output for URL http://localhost:8000/admin
<head>
<title>Site administration | My site admin</title>
<link href="/media/css/base.css" rel="stylesheet" type="text/css" />
<link href="/media/admin-media/css/base.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" href="/media/admin/css/base.css" />
<link rel="stylesheet" type="text/css" href="/media/admin/css/dashboard.css" />
But I always getting
<head>
<title>Site administration | My site admin</title>
<link href="/media/css/base.css" rel="stylesheet" type="text/css" />
<link href="/media/css/base.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" href="/admin-media/css/base.css" />
<link rel="stylesheet" type="text/css" href="/admin-media/css/dashboard.css" />
while direct accessing http://localhost:8000/admin-media/css/base.css shows css file from Python site-packages/django/contrib/admin/media/css
while direct accessing http://localhost:8000/media/admin-media/css/base.css shows css file from src/media/admin-media/css/
while direct accessing http://localhost:8000/media/css/base.css shows css file from src/media/css/
Important for Django 1.4 and newer (see here):
Starting in Django 1.4, the admin’s static files also follow this convention, to make the files easier to deploy. In previous versions of Django, it was also common to define an ADMIN_MEDIA_PREFIX setting to point to the URL where the admin’s static files live on a Web server. This setting has now been deprecated and replaced by the more general setting STATIC_URL. Django will now expect to find the admin static files under the URL <STATIC_URL>/admin/.
Previous answer, for older Django releases:
ADMIN_MEDIA_PREFIX is meant to be an absolute URL prefix, it has nothing to do with the MEDIA_URL - both can point to completely different points. Admittedly, the (bad) choice of "_PREFIX" in the name somewhat suggests that.
So, instead of {{ MEDIA_URL }}{{ADMIN_MEDIA_PREFIX}}css/base.css it must be {% admin_media_prefix %}css/base.css. And then you have to ensure that the web server serves the admin media files on '/admin-media/'.
Note that I used the admin_media_prefix tag above, which needs {% load adminmedia %} at the beginning of the template. The regular media context processor only gives you the MEDIA_URL variable, unfortunately.
In order to override the vanilla admin media serving, try something like this in your URLconf:
# A handy helper function I always use for site-relative paths
def fromRelativePath(*relativeComponents):
return os.path.join(os.path.dirname(__file__), *relativeComponents).replace("\\","/")
[...]
url("^admin-media/(?P<path>.*)$",
"django.views.static.serve",
{"document_root": fromRelativePath("media", "admin-media")})
Django 1.4 uses a new strategy for loading static media files, those using it will want to read over https://docs.djangoproject.com/en/dev/howto/static-files/
The executive summary of the above link is that two new settings variables, STATIC_URL and STATIC_ROOT, are used together with a newly included app (django.contrib.staticfiles) to collect and serve static files which are included on a per app basis.
When upgrading my django installation I had to set my STATIC_ROOT equal to my previous MEDIA_URL.
Under this system templates should now use {{ STATIC_URL }}.
Related
Following is the my static folder structure:
ProjectABC
- App1
- App2
- App3
- ProjectABC
- resources
- static
- imgs
- css
- templates
This is how the project structure looks like.
This is how the static configuration in settings.py looks like:
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
This is how the base.html looks like:
<!doctype html>
{% load static %}
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, maximum-scale=1, initial-
scale=1, user-scalable=0">
<meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?
family=Open+Sans:400,600,800">
<title>{{SITE_NAME}}</title>
<link rel='shortcut icon' href= "{% static 'img/favicon.png' %}"/>
<link href="{{STATIC_URL}}css/application.css" media="screen" rel="stylesheet"
type="text/css" />
<link href="{{STATIC_URL}}css/style.css" media="screen" rel="stylesheet"
type="text/css" />
<script src="{{STATIC_URL}}js/application.js" type="text/javascript"></script>
{% block extracss %}
{% endblock %}
</head>
<body>
<div class="container">
<h1>Hello</h1>
</div>
</body>
</html>
All the static files are being loaded as seen in terminal, but in browser its just loading bare html without any css or img:
Please lemme know if i am missing something.
Thnk You!
<link href="{%static 'css/style.css' %}" media="screen" rel="stylesheet"
type="text/css" />
add settings.py
STATIC_URL = "/static/"
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
Project App Url
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Static directory must be under the app itself. Create a 'static' directory directly to your app and for better practice, you can also create another directory to the static directory with your app name (static/[app_name]).
Also, {% load static %} must be above doctype in your base.html
change the static config in your settings.py to this:
STATIC_ROOT = os.path.join(BASE_DIR, 'resources/static')
STATIC_URL = 'resources/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'ProjectABC')
]
then run python manage.py collectstatic
and when you call the static file do it like this {% static 'css/style.css' %}
{% load staticfiles %}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="{% static 'search/style.css' %}">
This is the code and I am getting error in loading static file in this part
href="{% static 'search/style.css' %}
error cannot resolve directory "{%static search
Are your settings pointing to that (search/style.css)directory? You'll need something like the following in your app's settings.py:
...
STATIC_URL = '/search/'
STATIC_ROOT = os.path.join(BASE_DIR, 'search')
I have a little problem with Django , it is not my static files.
I have read the documentation , I tried putting the path directly , use {{ STATIC_URL }} and most recommended method is to leave the details. Any idea how to fix it ?
template.html:
{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static 'css/normalize.css' %}" />
<link rel="stylesheet" type="text/css" href="{% static 'css/demo.css' %}" />
<link rel="stylesheet" type="text/css" href="{% static 'css/component.css' %}" />
<link rel="stylesheet" type="text/css" href="{% static 'css/cs-select.css' %}" />
<link rel="stylesheet" type="text/css" href="{% static 'css/cs-skin-boxes.css' %}" />
local.py (settings)
STATIC_URL = '/static/'
STATICFILES_DIRS = [BASE_DIR2.child('static')]
my file folder in the right direction
The first folder is the virtualenv, the second folder is for Django Project where is the manage.py (image)
Views, models, urls and forms working properly, and display the chosen template
[The python code working properly (image)][2]
Update
I try change STATICFILES_DIRS for this:
STATICFILES_DIRS = (
os.path.join(os.path.dirname(BASE_DIR), 'static', ),
)
And for and printed the path to make sure it is correct, but the the error continues.
The result of print
Thanks in advance
I've resolved for Django 1.9 is: STATICFILES_DIRS = (BASE_DIR, 'static')
To start, I would make STATICFILES_DIRS a tuple.
Next, I would try a format like this:
STATICFILES_DIRS = (
os.path.join(os.path.dirname(BASE_DIR), 'static', 'static_dirs'),
)
To make sure it is going to the right directory, print the path and adjust accordingly:
print os.path.join(os.path.dirname(BASE_DIR), 'static', 'static_dirs')
Hope that helps!
I have solve that problem on mac by using:
STATICFILES_DIRS = [os.path.join(os.path.dirname(__file__), 'static').replace('\\','/'),]
I am using django-compressor to compress css files.
I did as it was explained in http://django-compressor.readthedocs.org/en/latest/quickstart/
I changed my template file as follows:
{% load staticfiles %}
{% load compress %}
<!DOCTYPE html>
<html lang="en">
<head>
{% compress css %}
<link href="{% static "crsq/css/zippednewsapp/bootstrap.readable.min.css" %}" rel="stylesheet">
<link href="{% static "crsq/css/zippednewsapp/zippednewsapp.css" %}" rel="stylesheet">
<link rel="stylesheet" href="{% static "crsq/css/zippednewsapp/typeahead.css" %}"/>
{% endcompress %}
</head>
.....
I do not see any change what so ever. No error. The files are not compressed. How do I change this?
In my settings file:
DEBUG = False
TEMPLATE_DEBUG = False
STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = (
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'compressor.finders.CompressorFinder',
)
Appreciate any help. Thanks
If html didn't change, I'd check server restart after the changes.
If not, you can increase logging level and see what does the compressor module print into the logs.
Also,the compressor should run under a user with enough privileges to create files and folders under COMPRESS_ROOT (which defaults to STATIC_ROOT)
Regards
I'm writing a django website and I want to use 'Semantic UI' for its front end. but when I add a Semantic UI Button to my first page in django, it only shows plain text!
the file tree of my project is like this :
Matab->
----Matab->
--------Templates->
--------------base.html
--------------login.html
---------settings.py
-----media->
--------css->
------------semantic.css
settigs.py :
MEDIA_ROOT = os.path.join(os.path.dirname(__file__),'../media/').replace('\\','/')
MEDIA_URL = '/media/'
base.html :
<html>
<head>
<link rel="stylesheet" href="{{ MEDIA_URL }}css/semantic.css"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="author" content="Navid" />
</head>
<body>
<div id="mainContent">
{% block content %}{% endblock %}
</div>
</body>
</html>
login.html:
{% extends 'base.html' %}
{% block content %}
<div class="ui button">hello</div>
{% endblock %}
Edit: Please use STATIC_ROOT and STATIC_URL instead of MEDIA_ROOT and MEDIA_URL. And follow the doc to setup the static file directories.
Your MEDIA_ROOT path is wrong. Update it to this -
MEDIA_ROOT = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'media')
Here os.path.dirname(__file__) evaluates to second Matab directory. Using os.path.dirname() again, takes it to First Matab directory. Then you just join it with media. Python automatically adds the slashes.
The problem was in urls.py, I should insert this line :
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)