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)
Related
I am trying to access static files in browser but unable to access them. My static settings insettings.py are below
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static")
My urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', FrontPage.as_view()),
# url('', include(router.urls))
]+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
My folder structure
projectname
projectname
settings.py
urls.py
app1_name
static
bs4
css
cover.css
admin
templates
My template.html
<link href="/static/bs4/css/cover.css" rel="stylesheet">
The above link isn't loading the static file. I'm getting 404 on hitting above url. Please let me know where am I going wrong.
All you need is an update of your settings:
STATIC_DIR = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = [STATIC_DIR, ]
Use: <link href="{% static 'bootstrap/css/cover.css' %}" rel="stylesheet">
Make sure to use {% load static %} at the top of your template.html and you are all set.
For some reason my django project doesn't see my static files, giving me 404 error, like so:
"GET /static/js/main.js HTTP/1.1" 404 1757
I do everything in accordance with the official guide. Here is my project structrure:
PMpro:
-PMpro (main project)
-users (app)
-tasks (another app)
-static
--js
--css
--etc...
-templates
--template files...
-manage.py
My settings.py file:
STATIC_URL = '/static/'
STATICFILES_DIR = [
os.path.join(BASE_DIR, 'static'),
]
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
urls.py file:
urlpatterns = [
path('admin/', admin.site.urls),
path('home/', TemplateView.as_view(template_name='index.html'))
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
In my .html file I's collecting static files and linking to corresponding css/js file like so:
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'css/organicfoodicons.css' %}" />
I'm pretty sure I do everything in accordance with the official django guide, and yet my project doesn't see/serve my static files.
I have already went through a number of similar problems people were posting here, and I have everything im my code exactly as it was advised, but the problem is still there.
Folks, any suggestions?
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.
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/
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.