I have done everything as always (I think), but for some reason django static files not working perfectly. I tried to find a solution but nothing helped.
This is the site now:
How it should be:
Inside static folder I have css, images, and so on. I have {% load static %} on the top of the html. I do not have any errors in the console.
One line of the html:
<link rel="stylesheet" href="{% static 'css/style.css' %}">
My settings.py:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static")
]
My folders:
SOLUTION: I have deleted pycache folders and the migrate folder,
then started the server again. It solved the problem. I do not know
the real reason, why...
I have deleted pycache folders and migrate folder. Now everything semms to be fine :) I do not know what was the reason of the problem.
Related
I have no idea what I'm doing wrong. I have STATIC_URL = '/static/' STATICFILES_DIR = [str(BASE_DIR.joinpath('static'))] under my settings.py. Here is an image to my current file structure https://pasteboard.co/K3uhtSN.png I linked with <link rel="stylesheet" href="{% static 'css/base.css' %}"> in base.html and loaded at the very top using {% load static %} Thank you
I had the same problem, but it solved by writing "staticfiles_dirs" like this:
STATICFILES_DIRS = ((os.path.join(BASE_DIR, 'static')), )
I had the same problem. If you had created a 'staticfiles' folder or directory during production then be sure to run python manage.py collectstatic in your command prompt or terminal.
DEVELOPMENT SERVER - Can't get my static files to work(CSS), I am not sure what I'm doing wrong but I cant seem to get them to load at all.
my HTML template
{% load static %}
<link type="stylesheet" href="{% static 'main.css' %}" />
Settings.py
STATIC_URL = '/static/'
STATICFILES_DIR = [
"C:/Users/Sam/Documents/Django/Blog/OrapiApplied/static"
]
My file layout
/letsdoit
manage.py
/app
/static
main.css
/letsdoit
settings.py
The setting is STATICFILES_DIRS, with an S.
If you add "django.contrib.staticfiles" to your INSTALLED_APPS setting in settings.py, the Django runserver development server will automatically look for all static directories in all your apps.
If you also have static files in other locations (e.g. if you were to add a static directory inside /letsdoit/letsdoit/ which is not an app), you'd have to add it to STATICFILES_DIRS so that Django also finds those.
Note that it's good pratice to namespace your static files for each app:
/app
/static
/app
image.jpeg
So that way you can add {% static 'app/image.jpeg' %} in your template and for a different app you'd be able to also have a image.jpeg which wouldn't conflict with the one in app: {% static 'other_app/image.jpeg' %}.
Add this lines to settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
and don't forget to clear cached images and data of browser before loading the page. Sometimes the browser just keeps giving the browser CSS from the cache.
I have read many posts about the topic and tried to follow all the suggestions (new to python here) but I am unable to get bootstrap to work in Django.
I have a project "myoffice" and an app "proposals" in it.
I have downloaded a css file and put it in following folder
django\bin\myoffice\proposals\static\bootstrap\bootstrap.min.css
I have made following changes to the settings.py
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')
STATICFILES_DIRS = (
os.path.join(
os.path.dirname(__file__),
'static',
)
STATIC_URL = 'proposals/static/'
and included static files in my template like this.
<!DOCTYPE html>
<html>
<head>
{% load staticfiles %}
<link href="{% static 'bootstrap/css/bootstrap.min.css' %}" rel="stylesheet" media="screen">
</head>
<body>
But it is still showing view without any formatting.
I figured it out. It was actually a stupid error. I discovered, I have to restart the webserver after adding css file to the static directory and making changes to the settings.py
I got an issue with Django 1.6:
I want to change the default static file directory in django. I don't want it in
project/myapp/static but in project/static
I readed django's documentation, added
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR,'static')
STATICFILES_DIR =(os.path.join(BASE_DIR, 'static'),)
In my settings.py
Then I ran ./manage.py collectstatic and files copyed as expected.
Finally I launched the server, the app is using django boilerplate plugin, so my template begins with:
{% extends 'dh5bp/base.html' %}
{% load url from future %}
{% load staticfiles %}
{% block head %}
<link rel="stylesheet" href="{% static "css/homepage.css" %}">
{% endblock %}
And the Css won't load: But in my server log, I got that:
[29/Aug/2014 11:23:03] "GET /static/js/dh5bp/plugins.js HTTP/1.1" 304 0
[29/Aug/2014 11:23:03] "GET /static/css/homepage.css HTTP/1.1" 404 1657
As you see the statics file from dh5bp (Boiler plate plugin) Are loaded correctly, while the statics from my app aren't loaded correctly.
I tried to add + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) to my urls.py, right after urlpatterns, It didn't worked.
So please if someone could tell me chat I'm doing bad and what should I change in the settings. It could be great
EDIT:
Did tryed the solution here, It give me the same result: only statics from boilerplate are loaded.
And not to mention that I've obviously checked if the files exists in /project/static, they does exists.
EDIT 2:
I tried to put the old static folder in my app, to ensure That he weren't looking for the files in the old folder. It doesn't, so I don't know where django expect those file to be? Is there a debug setup that could help on this?
Your STATIC_ROOT shouldn't be in STATICFILES_DIRS.
STATICFILES_DIRS should contain paths to your project's static files.
STATIC_ROOT is where all your static files are collected when you run collectstatic.
If you run django server with DEBUG=True, server will serve static files straight form STATICFILES_DIRS, and if DEBUG=False it won't handle static files at all. In that case you can run django server with option --insecure.
See this related question for more.
I can't seem to get my static files to load from my templates. I've followed the official documentation but I must be missing something.
My directory layout (generated by Django, most files omitted):
myproject
myproject
settings.py
urls.py
static
css
bootstrap.css
main.css
templates
base.html
myapp1
myapp2
...
manage.py
My settings.py:
STATIC_URL = 'static/'
I'm referencing my stylesheets like so (from my templates):
{% load staticfiles %}
<link rel="stylesheet" href="{% static "css/bootstrap.css" %}" type="text/css">
<link rel="stylesheet" href="{% static "css/style.css" %}" type="text/css">
Which gives this once rendered (in HTML):
<link rel="stylesheet" href="static/css/bootstrap.css" type="text/css">
<link rel="stylesheet" href="static/css/style.css" type="text/css">
Yet these links don't actually lead anywhere (when I visit them I get 404 error from Django). I feel that I could fix this by adding something in urls.py, but I thought Django did this automatically when you run the server? What am I missing?
Have you defined your static files directory in settings.py ?
I'm guessing you have 'django.contrib.staticfiles', in your installed apps.
If you haven't defined your static files dir, you could by doing something like this:
import os.path
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
STATICFILES_DIRS = (
os.path.join(PROJECT_ROOT, 'static'),
)
This is the working solution for static/media/template access in django for windows,
settings.py
import os.path
STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join('static'),
)
My problem was solved by adding "STATICFILES_DIRS" in settings.py file
STATIC_URL = '/static/'
STATICFILES_DIRS = ( os.path.join('static'), )
I thought Django did this automatically when you run the server?
Why did you think that? If you've followed the official documentation, you won't have found that. Read what you have to do to serve them in development here.
There's another problem. Your STATIC_URL is a relative link, so browsers add it to the existing page URL. So if you're on page /foo, 'static/css/style.css' evaluates to /foo/static/css/style.css'.
Make sure it either starts with / - ie /static/ - or is a full URL, ie http://myserver.com/static/.
Check if STATICFILES_FINDERS is defined in your settings.py
https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#std:setting-STATICFILES_FINDERS
The default value of STATICFILES_FINDERS is good enough but you have 2 choices :
you need to have the static file inside an app and having this app in your INSTALLED_APPS
or you need to define STATICFILES_DIRS with your path to the static files if expect the behavior being the one of django.contrib.staticfiles.finders.FileSystemFinder
I encountered this problem too. And I solved the problem by revising the href like this:
<html>
<link rel="stylesheet" href="{{STATIC_URL}}css/bootstrap.css" type="text/css">
<link rel="stylesheet" href="{{STATIC_URL}}css/style.css" type="text/css">
</html>
Make sure that you have the static folder set up in the right place, that is if it is in the app folder, then you can get further clarification from this helpful resource1.
My solution was DEBUG = True in settings.