I am trying to load static files in my django project. but it isn't loading. My static folder is in the base directory of project . Here I am giving some relevant code.
Here is my project's settings.py file's static part.
STATIC_URL = '/static/'
STATICFILES_DIR = [
os.path.join(BASE_DIR, 'static')
]
STATIC_ROOT = os.path.join(BASE_DIR, 'assests')
Here is the html part
{% load static %}
<img id="undraw_shared_workspace_hwky" src="{% static 'undraw_shared_workspace_hwky.png' %}">
My project name is 'webpage' and here is the image path.
webpage\static\undraw_shared_workspace_hwky.png
I have tried...
manage.py collectstatic
to collect the static files. It worked and gave me a assests directory inside or root directory.
But when I go to the website it doesn't load the image. and the console says "Failed to load resource: the server responded with a status of 404 (Not Found)" .
I am in a windows machine and using Django 3.0.
Now I need you help to fix this problem.
Thanks in advance.
Related
i am new to Django and web developement and am struggling with Static Files. I have seen this or similar questions in here before, but nothing seemed to bring me a solution. The Problem is: I can find the static files in Django, but they are not loaded within the template.
My Folder Structure is as follows (everything in a folder named Django):
media_root_folder
static_root_folder
Project
Project
manage.py
mysite
__init__.py
asgi.py
settings.py
urls.py
wsgi.py
templates
static
admin
Project
style.css
In Settings.py I have the following:
DEBUG = True
STATIC_URL = 'static/'
STATIC_ROOT = os.path.abspath(r'E:\LM\Django\static_root_folder')
STATICFILES_DIRS = [
BASE_DIR / "static"
]
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
'django.contrib.staticfiles' is added as well as Project to INSTALLED_APPS
I also even added this (I found in a Django Forum that this sometimes solves the Problem):
import mimetypes
mimetypes.add_type("text/css",".css", True)
Within urls.py i accordingly added
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Within my tempalte I am using:
{% load static %}
<link rel="stylesheet" type="text/css" href=" {% static 'Project/style.css' %} ">
If I run manage.py findstatic it finds the files and I also can open them directly in the browser. However if I manage.py runserver and load my site (even in incognito mode and after clearing cache and also in multiple browsers) the CSS is not used for the page at all. I only can see the plain html.
If I load the django/admin side the css is loaded and presented clearly.
What am I doing wrong and how can this be fixed?
Do you try to use collectstatic manage command?
$ python manage.py collectstatic
https://docs.djangoproject.com/en/4.0/ref/contrib/staticfiles/
The Problem was, that in every template I was extending the base.html .
When I was loading the CSS in base.html it worked without any problems.
I am trying to run my static files from the same serving site in production mode. This is what I did.
In setting.py, I have set,
DEBUG = False
ALLOWED_HOSTS = ['12.10.100.11', 'localhost']
I included, 'django.contrib.staticfiles' in INSTALLED_APPS
I set the Static root directory to,
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static")
I ran,
python manage.py collectstatic
to copy all the static files to a local serving directory.
(following: https://djangobook.com/serving-files-production/)
I then point static url directory to,
STATIC_URL = ("/static/")
I then run,
python manage.py runserver 12.10.100.11:8000
However, on console inspect the error shows:
**Get http://12.10.100.11:8000/static/css/main.css 404 (not found)**
on my base.html looks something like this:
{% load static %}
<link rel="stylesheet" href="{% static '/css/main.css' %}" type="text/css" />
I am new to Django and need lots of advice, thanks
Jef
1)Pip install whitenoise
2)Include the following lines in wsgi.py
from whitenoise.django import DjangoWhiteNoise
application = DjangoWhiteNoise(application)
You can't serve static files. django with DEBUG mode turned off isn't responsible for that. Ngnix is better option with chache mechanisms for that.
You should check out Heroku platform how they deploy production ready django projects with whitenoise for example. Github has a lot of examples of opensource projects powered with django that you can learn from
I am new in Django, previously I had a test site in which my static files were placed in static folders. There was a static folder for every app. But now I want to make a site, which will be deployed to python anywhere and so I have to have only one static folder.
This is the project folder: https://github.com/martin-varbanov96/fmi-fall-2016/tree/master/django/click_bait/miranda
You can see that in the settings file I have:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static")
in my target html file I have
<link rel="stylesheet" href="{% static 'home/css/style.css' %}" type="text/css">
which returns 404.
What is wrong how should I arrage my folders so that they are best working for the pythonanywhere host?
If you're getting a 404 error you should ensure debugging mode is on. If I assume it is on (as it is in the settings.py you have on GitHub), then your web server just isn't serving the static files. I would ensure you've run python manage.py collectstatic which will push the static files for the project to the static folder you've defined in settings.py.
I would also ensure that you've configured Python Anywhere to serve these files. According to their website (source: https://help.pythonanywhere.com/pages/DjangoStaticFiles/) you should ensure you have that static folder added so their server knows to serve the files. The steps at that link are as follows:
Go to the Web tab on the PythonAnywhere dashboard
Go to the Static Files section
Enter the same URL as STATIC_URL in the url section (typically, /static/)
Enter the path from STATIC_ROOT into the path section (the full path, including /home/username/etc)
I have a Django project with the following structure:
root
videos
static
templates
and the STATIC_URL setting in settings.py is STATIC_URL = '/static/'
I managed to use pyinstaller to create a windows executable from manage.py. I can start the Django server but I can't figure out where to put the static files.
When I first started the server it could not find the templates as well, it searched for them in : 'root\django\contrib\admin\templates\videos\' I copied the templates to this folder and it worked. But I can't figure out where to put the static files. I tried putting them in 'root\django\contrib\admin\static' to recreate the original structure. But it seems it doesn't search for them there...
Anyone knows where the static files should go? Or how to define the place where a bundled pyinstaller exe will look for them ?
please see https://docs.djangoproject.com/en/1.10/howto/static-files/
in your urls.py file, you should add something like blew
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^login$', login, name='login'),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
and in your app.spec file, add datas
datas=[('xxx/templates','xxx/templates'),
('xxx/static','xxx/static')],
I think I figured this one out. Like you, I was having the same issue where I could package and build my Django project with pyinstaller, but the static files could not be found when running the project from the built executable. Everything was working fine when I would run the project with manage.py, but this was a head scratcher.
The solution that worked for me was running the collectstatic function and then serving my static files from that directory instead of the app/static/app directory.
If you aren't familiar with collectstatic it essentially searches your project directory for all of the static files in your apps and puts them in a folder on your top level directory that you specify in your settings file.
Here's how to run it.
Here's a link to its documentation.
Now my top-level directory looked like this.
site
app
admin.py
apps.py
models.py
views.py
site
asgi.py
settings.py
urls.py
wsgi.py
media
media files
staticfiles
static files I want to serve (css, js, etc)
In my settings file I specified my static file settings like this...
STATIC_URL = '/staticfiles/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'app\\static\\app')
]
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
Then in my urls.py file I included the staticfiles directory like this...
(Note this is the urls.py file in the main site directory not in the app directory if you made one in there)
from django.contrib import admin
from django.conf import settings
from django.urls import include, path
from django.conf.urls.static import static
urlpatterns = [
path('', include('app.urls')),
path('admin/', admin.site.urls),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
In my spec file I was able to add the folder like this...
datas=[('..\\site\\staticfiles\\', '.\\staticfiles\\')]
And then in my html file I was able to use the static files like this...
<script src="{% static 'app/static.js' %}"></script>
<link rel="stylesheet" type="text/css" href="{% static 'app/static.css' %}">
Hopefully this helps some people if they've run into the same problem. Also as a reminder be sure to run collectstatic before you build especially if you've modified or changed any of your static files.
python manage.py collectstatic
Cheers!
First make sure that django.contrib.staticfiles is included in your INSTALLED_APPS in settings.py. Then have to insert in your settings.py for example this one:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
I hope definition of BASE_DIR you have in the top of settings.py. BASE_DIR points to a folder, where your manage.py file exists. So if you would have static files in the same dir, then you leave above setting as it is. If not, let's say in the same dir, next to manage.py you will have folder named app, then your settings should look like:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "app/static"),
]
And you can even run:
python manage.py collectstatic
To get admin static files into your static directory.
Hope that helps.
This looks like it might be an issue with where the PyInstaller packed python script looks for static files. They are placed in a temporary folder and need to be accessed by an absolute path. Check out this issue: Bundling data files with PyInstaller (--onefile)
I am learning django and I already have a bit noobish question. I can not point to my static folder and I tried all the combinations, watched people do it in youtube tutorials etc.
My settings.py looks something like this:
STATIC_ROOT = '/home/peter/brewery/static/'
TEMPLATE_DIRS = '/home/peter/brewery/mysite/templates/'
where brewery/ is the folder containing mysite/ and static/, mysite/ is the folder created by
django-admin.py startproject
where settings.py also lives...
It seems that templates folder is mapped correctly, since the page renders with proper templates, it just cannot access the css in the /static/css/ folder. I show the path in my template for css like this
<link ... href='/static/css/brewery.css' />
I have also tried to make href absolute path on my computer and it does not work.
I am using django 1.3 and am running the server provided by django (python manage.py runserver)
1 - In your settings file, define a static url and static root like this:
STATIC_URL = '/static/'
2 - Set DEBUG = True
3 - Make sure your TEMPLATE_CONTEXT_PROCESSORS variable includes django.core.context_processors.static.
4 - Reference it in your templates like...
<link ... href='{{ STATIC_URL }}css/brewery.css' />
Source: https://docs.djangoproject.com/en/dev/howto/static-files/
In debug mode STATIC_ROOT is not used, but staticfiles_urlpatterns() provides static files from all different apps. Put your static files either into static/ directory in one of your apps or define STATICFILES_DIRS in your settings and put static files for the site there.
STATIC_ROOT is just the location where all static files are collected from all apps and STATICFILES_DIRS when you call:
python manage.py collectstatic
And it is only used in production environment.