Django Static Files - CSS File Won't Load - python

I am learning Django, and am trying to load a static css file.
I have seen the other questions and read the docs, but I am still unable to see the problem.
I am using Django 1.11.
Here is my urls.py:
from django.conf.urls import url
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
from . import views
urlpatterns = [
url(r'^$', views.index),
url(r'^admin/', admin.site.urls),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
My settings.py (only the part to do with static files):
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "/static/")
STATICFILES_DIRS = (
STATIC_ROOT,
)
And the part of my template where I try and load the files:
{% load static %}
Whenever I load the index.html template, the following error messages are displayed by my console:
core.js:5 Uncaught ReferenceError: define is not defined
at core.js:5
localhost/:12 GET http://localhost:8000/static/css/homepage.css
localhost/:11 GET http://localhost:8000/static/css/horz-navbar.css
localhost/:10 GET http://localhost:8000/static/css/fonts.css
localhost/:13 GET http://localhost:8000/static/css/style.css
Here is the file directory structure:
mysite
db.sqlite3
manage.py
mysite
__init__.py
settings.py
urls.py
views.py
wsgi.py
static
admin
css
fonts.css
horz-navbar.css
homepage.css
style.css
templates
index.html
So Django doesn't seem to recognize that the files exist, I have checked and made sure that the files exist on my computer, and I'm not sure if this was needed, but I have also run python manage.py collectstatic
Please tell me if you need anymore information.

Change your STATIC_ROOT into another name and then update your STATICFILES_DIR. Something like this:
STATIC_ROOT = os.path.join(BASE_DIR, 'static_files')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)

Replace "{% static "css/fonts.css" %}" with "{% static 'css/fonts.css' %}". There's a mismatch between quotations.

Related

404 Static file not found - Django

I have an issue with django. I recently bought an instance of a shared server and I wanted to move my django website from AWS to this server (which use Cpanel). All worked fine with AWS but when I switched to Cpanel all statics files were missing.
this is my settings.py:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = "/media/"
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
my project structure:
my_project
|-app/
|-...
|-views.py
|-db.sqlite3
|-manage.py
|-media/
|-my_project/
|-...
|-settings.py
|-static/
|-main_page/
|-js/
|-my-script.js
I add static files like this:
{% load static %}
<script src="{% static 'main_page/js/my-script.js' %}"></script>
This is the error:
GET http://my.domain.com/static/main_page/js/my-script.js net::ERR_ABORTED 404 (Not Found)
When I go to the URL of the file it understands it like one of my URLs:
I hope you will help me to solve this issue ;)
thanks.
you need to add the static & media files config in the urls.py , like this
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
the django docs : https://docs.djangoproject.com/en/3.1/howto/static-files/

Django: serving user uploaded files

I'm new to Django and Python.I want to display a link to my files stored in my static folder.Here is the required code from settings.py:
MEDIA_ROOT = os.path.join(BASE_DIR,'static')
MEDIA_URL = "/media/"
When I access these files through my admin I get the following url displayed in my browser:
http://127.0.0.1:8000/media/filename.pdf
Hence what I tried is to give a link to the file in my HTML code :
File
But instead of displaying http://127.0.0.1:8000/media/filename.pdf it just displays /media/filename.pdf which results in an error.
Adding localhost:8000/ before {{instance.docFile.url}} also didn't work.
Why isn't this working?What is the correct way to display link to my files?
Let me know if anything else is required.Thank you.
in your urls.py
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
in your settings.py
MEDIA_ROOT = os.path.join(BASE_DIR, 'uploads')
MEDIA_URL = '/uploads/'
Then in your template
File
If you want know refer the docs Manage static files
The error is coming because your file is not stored in media.it is stored in static but your database store media URL. change your MEDIA_ROOT (given below) and try to upload one more time
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
Don't put slash
:
File
Please see the Django setting MEDIA_ROOT documentation to better understand what your trying to do.
First MEDIA_ROOT and STATIC_ROOT must have different values.
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
MEDIA_URL = '/media/'
See static files documentation for more details.
Second, You need to add these settings to your base urls.py.
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
If you want to use {{ MEDIA_URL }} in your templates, add 'django.template.context_processors.media' in the 'context_processors' option of TEMPLATES in settings.py.
At the top of your HTML page add {% load static %} then to display your media:
<img src="{{ MEDIA_URL }}{{ docFile }}" alt="test">
Please take your time with the Django Documentation, trust me it will help and also checkout the File upload settings

Get the file path for a static file in django code

I have a django application that sends off an email containing image files as MIME attachments. The emails are sent from views.py, but in order to attach the files, I need to get the full path name of the image(s), so python can open them. These files are in the static folder in my app, but I can't seem to find a way that I can get the full path of the file on the filesystem that works in development mode - It works fine in production after collecting static, but in dev, it can't find the file as the static files are served from individual app folders in development.
Use finders-module of Django
from django.contrib.staticfiles import finders
result = finders.find('css/base.css')
searched_locations = finders.searched_locations
String result is the file-system path, and if not found, double check searched_locations
project_dir.url add to the end of file
if DEBUG:
urlpatterns += patterns(
'',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': MEDIA_ROOT}),
url(r'^static/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': STATIC_ROOT}),
)
project_dir.settings
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
STATIC_ROOT = os.path.join(BASE_DIR, 'static_debug')
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
make dirs media & static_debug (add them to .gitignore)
project_dir/
static/
image.jpg
settings.py
urls.py
apps/
__init__.py
some_app/
static/
some_app/
css/
style.css
media/
static_debug/
Now you can run project or directly
python manage.py collectstatic
access from views
from django.templatetags.static import static
static('image.jpg')
static('some_app/css/style.css')
access from templates
{% load staticfiles %}
{% static 'image.jpg' %}
{% static 'some_app/css/style.css' %}
After doing lots of mistakes the following thing worked for me..
Suppose you have following directory structure (Thanks #madzohan)
project_dir/
static/
image.jpg
settings.py
urls.py
apps/
__init__.py
some_app/
static/
some_app/
css/
style.css
And now if you want to get the path of project_dir/static/image.py then
In your project_dir/settings.py file define a STATIC_DIR varaible on the file scope like. ( If not done before )
project_dir/settings.py
import os
# after your other file variables
STATIC_DIR = os.path.join(BASE_DIR, 'static')
Now in your app_name/views.py file from where you want to acess the /static/image.jpg file
app_name/views.py
import os
from project_dir.settings import STATIC_DIR
# here I want the /static/image.jpg file then
image = os.path.join(STATIC_DIR, 'image.jpg')
And then you can have acces to image.jpg file throught image variable
Hope this works well for you
Since you want an app's static, here's what you need!
from django.contrib.staticfiles import finders
APP_LABEL = 'app_youre_looking_at'
FILE_NAME = 'file_name_you_want_to_read_or_write.ext'
stores = finders.AppDirectoriesFinder(app_names={APP_LABEL}).storages
print(f'Here it is: {stores[APP_LABEL].path(FILE_NAME)}')
The FILE_NAME is not required to exist. You can use stores[APP_LABEL].path('') to get path only.

django static files relative path does not work

I cahnged the static files defualt path
and added in url.py
urlpatterns += patterns('',
url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),
)
settings.py
STATIC_ROOT = os.path.join(BASE_DIR, "static")
It works fine for first level pages
like
/login
/admin
/dahboard
but static files does not load when I go to second level pages
/admin/users/
/admin/users/add/
How I can fix this problem
Make sure your STATIC_URL has a / at the beginning:
STATIC_URL = '/static/'
Otherwise, URL will be like <img src="static/thing.png" /> which can work on first level (since it will search from the root) but not when you are in subdirectories.
Be aware the serve method only work in DEBUG mode.

How to work properly with django static files

i have a problem with django static files handling.
I am confused about how to use MEDIA_ROOT, STATIC_ROOT, MEDIA_URL and STATIC_URL
I have a file structure like this(sorry, i dont know how to indent properly :S):
static/
css/
img/
js/
For example, if I have a .css file in my css dir, how could I reach it?
MEDIA_ROOT and MEDIA_URL are deprecated. Use STATIC_ROOT and STATIC_URL.
STATIC_ROOT = "Absolute path to your static directory" (Example: /home/your_app/static)
STATIC_URL = "/static/" (Could be anything you would like to use)
When you want to serve static content pass the context_instance and in your template use,
{{STATIC_URL}}/file_path_from_static_directory/
EDIT:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()

Categories