Bootstrap does not be set to my Django app.In google console,Failed to load resource: the server responded with a status of 404 (Not Found) bootflat.min.css error happens.I wrote in settings.py
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DEBUG =True
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
STATICFILES_DIRS = [os.path.join(BASE_DIR,'bootflat.github.io'), ]
in PythonServer_nginx.conf
server {
# the port your site will be served on
listen 8000;
# the domain name it will serve for
server_name MyServerIPAdress; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
location /static {
alias /home/ubuntu/PythonServer/PythonServer/accounts/static; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /home/ubuntu/PythonServer/uwsgi_params; # the uwsgi_params file you installed
}
}
I do not know why Bootstrap does not be set to my Django app.I run command python manage.py collectstatic but no error happens.How should I fix this?What should I write it?
I assume the project works fine in local, this problem occurs only when you use nginx. If that is the case probably the path you mentioned in the nginx config for location /static is wrong, try getting the absolute path to the static folder from the server and see.
I have no way to test my answer for obvious reason, but your STATICFILES_DIRS and STATIC_ROOT don't match. If you change your STATICFILES_DIRS to [os.path.join(BASE_DIR,"/static/"),'https://bootflat.github.io'], it might work. I suggested so because it doesn't make sense to me to os.path.join a local path with a public URL.
Try this,
In settings.py:
STATIC_DIR = os.path.join(BASE_DIR,"static")
Now at last, add these lines:
STATIC_URL = '/static/'
STATICFILES_DIRS = [STATIC_DIR,]
Now, put all your bootstrap files in a 'static' folder under your project folder.
At your HTML code,
You have to load the static files using:
{% load staticfiles %}
Now,you can use your static files using:
<link rel="stylesheet" href="{% static "assets/css/bootstrap.min.css" %}">
P.s. my 'bootstrap.min.css' is present under 'static' folder and then 'assets' and then 'css' folder.
It works perfectly for me.
First: where are you storing your static bootstrap files? I recommend storing them in the app's directory where you are referencing them in a template. For example, if you have an app myapp I would put them in /myapp/static/myapp/. So if I'm storing bootstrap.css it would go in /myapp/static/myapp/bootstrap.css.
Next, in your template you need to reference the correct static folder. For the Bootstrap stylesheet, the href would look like href = "{% static 'myapp/bootstrap.css' }%"
Lastly, in your settings.py file add the line STATIC_ROOT = '/home/ubuntu/PythonServer/PythonServer/accounts/static' (based on what you wrote, just make sure that it's pointing to the correct deployment folder where you're keeping your live static files) and run collectstatic again.
First of all you do not need STATICFILES_DIRS since django will collect static files for you from each INSTALLED_APP specified in your settings.py. You need to add bootstrap4 to INSTALLED_APP:
INSTALLED_APPS = [
...
'my_great_app',
'bootstrap4',
]
You need to specify STATIC_ROOT and STATIC_URL as you do in your settings.py. You need to understand why you use each setting.
Django uses STATIC_ROOT in collect_static command which collects
all static files from apps and directories in STATICFILES_DIRS puts
in STATIC_ROOT. For example you created a new app which has it's
required static files in itself, you run ./manage.py collect_static
to put your static files in STATIC_ROOT in which web server (nginx
in your case) serves them. At your installation STATIC_ROOT should be
showing to /home/ubuntu/PythonServer/PythonServer/accounts/static
as seen in your nginx config.
STATIC_URL is used to create static file urls when you use
static template tag. Since the files are served by nginx you need a
specific path, host etc.
STATICFILES_DIRS is used to add static files which are not residing
in apps folders. I do no recommend its use for your case.
On your template you need to use something like the following:
{% extends 'bootstrap4/bootstrap4.html' %}
{% load bootstrap4 %}
{# Display a form #}
<form action="/url/to/submit/" method="post" class="form">
{% csrf_token %}
{% bootstrap_form form %}
{% buttons %}
<button type="submit" class="btn btn-primary">Submit</button>
{% endbuttons %}
</form>
Taken from django-bootstrap4 github repo.
By extending bootstrap4/bootstrap4.html you would get required css and js files in your html. With {% load bootstrap4 %} you can use bootstrap4 templatetags and filters.
Try by adding following code in your url.py
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += patterns('', (
r'^static/(?P<path>.*)$',
'django.views.static.serve',
{'document_root': settings.STATIC_ROOT}
))
Add static root variable in settings.py as shown by Luke B
STATIC_ROOT = '/home/ubuntu/PythonServer/PythonServer/accounts/static'
Check here documentation https://docs.djangoproject.com/en/2.0/howto/static-files/
Related
I have just started learning DJango and am trying to use css file for my html template. As per the documentation, I have added the STATIC_URL and STATIC_ROOT to my settings.py file as follows:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
BASE_DIR is already set as follows:
BASE_DIR = Path(__file__).resolve().parent.parent
In the template, I am using the css file as follows
{% load static %}
<link rel="stylesheet" href="{% static 'style.css' %}">
and the style.css is present at location DJango_App/static, DJango_App being my project directory with the manage.py file. Still I am getting error
"GET /static/style.css HTTP/1.1" 404 1653
DEBUG is set to True
Directory structure is:
DJango_App
|->DJango_App
|->(settings.py, urls.py, views.py, etc)
|->templates
|->(html templates)
|->static
|->style.css
How do I resolve this?
Update:
Looking at your directory structure above, your static files are not under a Django app. In that case, you should also set STATICFILES_DIRS, see:
https://docs.djangoproject.com/en/3.1/howto/static-files/#configuring-static-files
https://docs.djangoproject.com/en/3.1/ref/settings/#std:setting-STATICFILES_DIRS
Original:
Looks like you did not add the static url handler to urlpatterns.
Serving static files during development require add to urls.py:
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'm trying to create a Django app and i have made all the static settings like
Settings.py
STATICFILES_DIRS = [
os.path.join(BASE_DIR,'CricketTeamManagement/static')
]
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
URLS.py
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('Management.urls'))
] + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
I have also created a static folder in Main project folder and ran python manage.py collectstatic it created a new static folder outside and it has admin folder in it and sub folders like js,css,font,images and collectstatic collects the images and stores in this folder. I see static files are clearly working.
All the models.py imagefields are uploaded to media folders and even that works.
The Problem comes here
My css files are not taking its styles and not altering my html elements.
If I have the below code in html file the styling works and if i separate it to a css folder and create a css file by removing style tag it is not working.
<style>
body {
background-color: powderblue;
}
h1 {
color: blue;
}
p {
color: red;
}
</style>
when i saw the css file Not Found error it got fixed with
to make html check the css file.
Unsure what is the issue. Thanks in Advance.
Update
Project Format
Link Tag in HTML
The href in your index.html right now refers to yourwebsite/stylesheet.css. Django doesn't know where that URL is as you defined your static URL as "/static/". Static located in the STATIC_DIRS will be loaded under the URL yourwebsite/static/stylesheet.css. This is why you are getting the 404 on the CSS file.
Change your index.html to use Django built-in template tags to make it easier.
{% load static %}
Some code here.....
<link rel="stylesheet" type='text/css' href="{% static 'css/stylesheet.css' %}">
This will automatically generate the right url for your static file which should solve the problem.
You don't need to run collect static in development as django's server will take care of the static files. During production will will need to run collect static and point your reverse proxy or the like towards the static files.
you have to link your css files this way in templates, also collectstatic command is not required during production stage only
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'css/style.css' %}"/>
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 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 am learning Django and trying to create modular templates and I am coming across this issue
In my Developer tools I am getting this error:
GET http://127.0.0.1:8000/static/assets/css/default.css 404 (NOT FOUND) 127.0.0.1/:8
GET http://127.0.0.1:8000/static/assets/images/pythonlogo.jpeg 404 (NOT FOUND) 127.0.0.1/:84
From my understanding, having STATIC_URL = '/static/' allows {% static %} to be used and also appends /static/ to the path of your static folder. Also, using STATICFILES_DIR is for locating the static files in your project.
Currently I have:
STATIC_URL = '/static/'
STATICFILES_DIR = (
('assets', '/Users/BobDole/Development/django-brad/django_test/'),
)
From reading the documentation, it seems to me that 'assets' is used as a namespace or a variable to represent /Users/BobDole/Development/django-brad/django_test/
In my html page I used
<img src="{% static 'assets/images/pythonlogo.jpeg' %}">
<link rel="stylesheet" type="text/css" href="{% static 'assets/css/default.css' %}">
My current project directory structure
django_test/
admin/
article/ <-- app
templates/
django_test/
templates/
images/
static/
css/
I believe that I am using STATIC_URL and STATICFILES_DIR improperly could some provide me with some suggestions? Thank you!
try this:
<img src="{{ STATIC_URL }}assets/images/pythonlogo.jpeg">
Django will change STATIC_URL for /whatever/whatever/static/, so the url the img will access is: /whatever/whatever/static/assets/images/pythonlogo.jpeg
The thing is /whatever/whatever/static/ has to be the path until the "static" folder inclusive
I use in my projects STATIC_URL like this(settings.py):
PROJECT_PATH = os.path.abspath(os.path.dirname(__file__))
STATIC_ROOT = os.path.join(PROJECT_PATH, "static")
STATIC_URL = '/static/'
EDIT You have to add this 3 lines in your settings.py
What makes you think assets is magic in some way? It's not, it's simply the name of a directory. You don't have a directory called that, so you shouldn't use it. Use {% static 'images/pythonlogo.jpeg' %} etc.