Django template can't see CSS files - python

I'm building a django app and I can't get the templates to see the CSS files... My settings.py file looks like:
MEDIA_ROOT = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'media')
MEDIA_URL = '/media/'
I've got the CSS files in /mysite/media/css/ and the template code contains:
<link rel="stylesheet" type="text/css" href="/media/css/site_base.css" />`
then, in the url.py file I have:
# DEVELOPMENT ONLY
(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': '/media'}),
but the development server serves the plain html (without styles). What am I doing wrong?
--
OK - I got it working based on what you folks have said. The answer is:
settings.py:
MEDIA_ROOT = 'd://web//mysite//media//' #absolute path to media
MEDIA_URL = '/mymedia/' #because admin already using /media
site_base.html:
<link rel="stylesheet" type="text/css" href="/mymedia/css/site_base.css" />
urls.py
from mysite import settings
if settings.DEBUG:
urlpatterns += patterns('',
(r'^mymedia/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT}),
)
And voila! It works.

in the "development only" block in your urls.py you need to change
(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': '/media'}),
to...
(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT}),

ADMIN_MEDIA_PREFIX is set to \media\ by default, and is probably 'stealing' the path. Change that setting, or use a different one for non-admin media - eg site_media or assets.

On the dev server, I like to cheat and put the following in my urls.py
if settings.DEBUG:
urlpatterns += patterns('',
(r'^includes/(?P<path>.*)$', 'django.views.static.serve', {'document_root': '/path/to/static/files'}),
)
That way anything in the project under the "/includes" folder is server by the dev server. You could just change that to "/media".

It also worked for me, thanks guys !!
settings.py
MEDIA_ROOT = '/home/pi/ewspaces/ws-classic/xima/media'
MEDIA_URL = '/statics/'
urls.py
if settings.DEBUG:
urlpatterns += patterns('',
(r'^statics/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT}),
)
inside templates:
<link type="text/css" href="/statics/css/base/jquery.ui.all.css" rel="stylesheet" />

I had a similar problem when I was trying to get jQuery to work. My fix was to add an alias to my Apache httpd.conf file that pointed to the folder containing the .js. You could do the same with your CSS folder.

Related

Unable to display static images and load CSS in Django

Using pycharm 2019.2.5,
python 3.7,
Django 2.2.5,
I am creating a website by importing a template and creating apps for it through django, but I can't get any of the static files to work. When I run the dev server, none of the fonts/css/js/images display. Also I can't import google fonts from my HTML file. I've read all the answers and I'm posting the pertinent data i'm aware of. This seems to be a fairly common question, and I've tried all their solutions. Let me know if you need more data.
YSMR *edited to add. #This is my top-level project created in pycharm.
YSMR
\blog
\contact
\schedule
\sendemail
\static
\css # I plan on adding namespacing for this later
\fonts
\js
images....
\templates
\index.html
additional templates...
__init__.py
settings.py
urls.py
views.py
wsgi.py
index.html below. I can view the file, but again, the embedded static files and font do not execute. The first several lines I've loaded below.
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<title>YSMR</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=EB+Garamond:400,400i,500,500i,600,600i,700,700i&display=swap" rel="stylesheet">
<link rel="stylesheet" href="{% static "css/open-iconic-bootstrap.min.css" %}">
<link rel="stylesheet" href="{% static "css/animate.css" %}">
Settings.py
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
ENV_PATH = os.path.abspath(os.path.dirname(__file__))
STATIC_ROOT = os.path.join(ENV_PATH, '/static/') #I've tried to change 'static' to something else, but I get an 'unresolved reference' error when I do.
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "YSMR/static"), #I get an 'unresolved reference "static"' when I remove the YSMR
]
When I remove the YSMR from the STATICFILES_DIRS path, I get an "unresolved reference 'static'" warning
also, django.contrib.staticfiles is included in INSTALLED_APPS
Thanks in advance.
Change your settings as follows and try:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_cdn")
MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "media_cdn")
Also make sure that you added these lines to your main url file.
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Change your settings as follows and try:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "app_name/static_root"),
]
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "app_name/static")
MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "media")
Also make sure that you added these lines to your main url file.
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

static dir not found django

I'm a beginner in using django and tryin to make my first app, but I keep on getting "Not found" every time I add a javascript file on my view
This is my setting.py
STATIC_URL = '/home/me/PycharmProjects/GLife/static/'
MEDIA_ROOT = os.path.join(PROJECT_DIR, 'media')
MEDIA_URL = '/media/'
STATIC_ROOT = 'static/'
STATICFILES_DIRS = (
os.path.join(PROJECT_DIR, 'static'),
)
urls.py
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^register', include('register.urls')),
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += staticfiles_urlpatterns()
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<!--<script src="/static/djangular/js/django-angular.min.js" type="text/javascript"></script>-->
<script src="{% static '/js/_register/register.js' %}" type="text/javascript"></script>
<title></title>
</head>
<body>
I've already search about it but the suggestions failed, hope anyone could help
here's my project structure
myproject
--main
--register
---migrations
---templates
--static
---js
----_register
I'm just trying to make an alert display as a testing using js
This is happening because you switched the values of STATIC_ROOT and STATIC_URL. It must be like this:
STATIC_URL = '/static/'
STATIC_ROOT = '/home/me/PycharmProjects/GLife/static/'
STATIC_URL is the URL to use when referring to static files located in STATIC_ROOT.
STATIC_ROOT is the absolute path to the directory where collectstatic will collect static files for deployment.
Also, your static and STATIC_ROOT paths must be different.
When using the development server you don't need to configure STATIC_ROOT or use collectstatic management command because it will automatically serve the static files from static folder if DEBUG is true.

Django 1.7 - Serving static files

I'm following the official documentation in order to serve static files but I'm recieving error 404 in the development console. I'm using 'django.contrib.staticfiles', so static files should be automatically served. This is my setup:
Settings:
STATIC_ROOT = ''
STATIC_URL = '/static/'
Template header:
{% load staticfiles %}
<link rel="stylesheet" href="{% static "css/app.css" %}">
Directory tree:
django_project
\apps
\static
\css
app.css
\templates
index.html
I can see in firefox console that the path to my file is correct:
So the problem must be that Django is not serving static files. I can't find what I'm missing. Any advice is more than welcome.
SOLUTION:
I was missing this line in my settings.py
STATICFILES_DIRS = (os.path.join(os.path.dirname(__file__),'static'),)
It looks it's mandatory, same as TEMPLATE_DIRS.
When you run collectstatic it puts all your static content in the path specified by STATIC_ROOT. Check the deploy to production docs
If you are using the django server try checking the path that is being generated by {% static %}...you may have some trailing slash or something missing.
Check that you have are following all the requirements. You need to have django.contrib.staticfiles in your installed apps and something like this in your main urls file:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = patterns('',
# ... the rest of your URLconf goes here ...
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
That should work :)
settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'project', "static"),
)
example of context_processors from settings.py:
TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.static",
'django.core.context_processors.request',
"django.core.context_processors.tz",
"django.contrib.messages.context_processors.messages",
)
example of installed apps in settings.py:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
)
urls.py:
from django.conf import settings
from django.conf.urls.static import static
if settings.DEBUG:
urlpatterns + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
For anyone running django-cms and experiencing 404 errors (particularly, all of your static files are having "en-us" prepended to the URL), I found the following steps to help.
First, turn off internationalization of pattern matching in your urls.py file, as described here:
urlpatterns = i18n_patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('cms.urls')),
)
should instead be:
from django.conf.urls import patterns
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('cms.urls')),
)
The import is important, because the configuration of django-cms removes the patterns import from django.conf.urls.
This solved the redirect, but it still wasn't finding my static files. I needed to manually add the static url to the url patterns, like this:
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('cms.urls')),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
After that, static files worked as expected.
I'm sure that this is probably related to me messing up my configuration as a complete novice to Django. But since others might have the same problems, I'm putting it out there as a possible, if less than ideal, solution.

django adding part of url to static css path

I have my css located in
static/css/boostrap.css
I currently have 2 views. A login view and a dashboard view.
urls.py
from django.conf.urls import patterns, include, url
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
url(r'^$', 'logins.views.login', name='login'),
url(r'^dashboard', 'dashboards.views.dashboard', name='dashboard'),
url(r'^admin/', include(admin.site.urls)),
)
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
When i load up the login page it looks for the static files in
/static/css/login.css
But when i load the dashboard it looks for it in
dashboad/static/css/bootstrap.css
login.html css reference
<link href="static/css/signin.css" rel="stylesheet">
dashboard.html css reference
<link href="static/css/dashboard.css" rel="stylesheet">
Its adding part of the URL to the path off the static files and can not for the life of me figure out how to stop it.
settings.py
STATIC_URL = '/static/'
TEMPLATE_DIRS = (
'/Users/chrismeek/Documents/Python/virtual/src/static/templates',
)
if DEBUG:
MEDIA_URL = '/media/'
STATIC_ROOT = '/Users/chrismeek/Documents/Python/virtual/src/static/static-only'
MEDIA_ROOT = '/Users/chrismeek/Documents/Python/virtual/src/static/media'
STATICFILES_DIRS = (
'/Users/chrismeek/Documents/Python/virtual/src/static/static',
)
You are using relative URLs to link to the assets: so they always start from the current page's directory.
Make sure you always use a leading slash:
<link href="/static/css/dashboard.css" rel="stylesheet">
Even better, use Django's static tag to automatically output the value of STATIC_URL, whatever it happens to be:
{% load static %}
...
<link href="{% static "css/dashboard.css" %}" rel="stylesheet">

Django Static Files - CSS not working [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Django and Serving Static Files
I am with one problem to load CSS at base.html. I put all css files at the directory /static.
At urls.py I put this code:
if settings.DEBUG:
urlpatterns += patterns('',
(r'^static/(?P<path>.*)$', 'django.views.static.serve',
{ 'document_root': '/home/bkcherry/botstore/botstore/static' }),
)
And at base.html i put the following:
<link rel="Stylesheet" type="text/css" href="/static/css.css" />
When I go to the main.html, the css style is not working. I need to configure the settings.py MEDIA_ROOT, MEDIA_URL or STATIC_ROOT?
you must not use MEDIA_ROOT or MEDIA_URL this is for uploaded media not your static content, and you do not need to setup URL patterns as that is only for django 1.2 or " if you are using some other server for local development": https://docs.djangoproject.com/en/dev/howto/static-files/#serving-static-files-in-development
you need to have your static files in:
botstore/botstore/static/botstore/css.css
then use:
HOME_ROOT = os.path.dirname(__file__)
# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"
STATIC_ROOT = os.path.join(HOME_ROOT, 'staticfiles')
# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'
# URL prefix for admin static files -- CSS, JavaScript and images.
# Make sure to use a trailing slash.
# Examples: "http://foo.com/static/admin/", "/static/admin/".
ADMIN_MEDIA_PREFIX = '/static/admin/'
# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
then in your HTML you can refer to your static files thus:
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}botstore/css.css" />
I think you need a slash at the end of your path, ie '/home/bkcherry/botstore/botstore/static/'
If you check official documentation
from django.conf import settings
# ... the rest of your URLconf goes here ...
if settings.DEBUG:
urlpatterns += patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT,
}),
)
MEDIA_ROOT should have / on the end (https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-MEDIA_ROOT)

Categories

Resources