Using a readymade Template for django frontend - django

I tried using a readymade HTML5 template for my Django Project and updated the static links and all but it fails to load the CSS and js and other static files too.
My folder structure :
**MyProject
apps
templates
MyProject
static
MyProject
css
js
etc**
My Settings for static :
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static")]
TEMPLATES = [
......
'DIRS': [os.path.join(BASE_DIR, 'templates'),
os.path.join(BASE_DIR,'static') .
..... ]
I have tried
using the collectstatic command.
Adding STATICFILES_DIRS.
Adding this code snippet to urls.py
Trace from terminal:
[04/Apr/2020 13:04:51] "GET /static/css/owl.theme.default.min.css HTTP/1.1" 404 1814
[04/Apr/2020 13:04:51] "GET /static/css/animate.css HTTP/1.1" 404 1772
[04/Apr/2020 13:04:51] "GET /static/css/magnific-popup.css HTTP/1.1" 404 1793
[04/Apr/2020 13:04:51] "GET /static/css/aos.css HTTP/1.1" 404 1760
[04/Apr/2020 13:04:51] "GET /static/css/owl.carousel.min.css HTTP/1.1" 404 1799

collectstatic will collect static resources that you have placed in your individual apps and copy them to an app directory within the project static directory. So, if you have an app static directory like this:
my_app
static
my_app
css
js
etc
your project static dir will look like this after collectstatic:
static
my_app
css
js
etc
You can also manually place static resources in the project static directory. This is a good place for your project wide css, js, and image files.
static
css
images
js
From the 404 errors you posted, the server is looking for your css files in the static/css/ directory and you have your css directory inside the MyProject directory. Move your css directory out one level and place your css files there and it should work.

static
MyProject
css
js
Your Error:
"GET /static/css/owl.theme.default.min.css HTTP/1.1" 404 1814
according to your error server is looking your file in static folder and then in css while your files is in static/MyProject/css
so, update your code in your html file
{% static 'MyProject/css/owl.theme.default.min.css' %}

Related

uwsgi static files loading error form django (react and webpack)

My app consists of django and react, which at the end is bundled to static (.js) file.
My goal is to setup a nginx for it, but for now I just want to run in uwsgi to see if it works.
Aaaaand it does not work at all. I belive there is a issue with loading this bundle file which webpack is compiling.
Connection to uwsgi works, server is up and running, but when connecting to 127.0.0.1:8080 I get (in Mozilla):
The resource from “http://127.0.0.1:8080/static/frontend/main.js”
was blocked due to MIME type (“text/html”) mismatch
(X-Content-Type-Options: nosniff).
# and another
GET http://127.0.0.1:8080/static/frontend/main.js
HTTP/1.1 404 Not Found
Content-Type: text/html
X-Frame-Options: DENY
Content-Length: 3277
X-Content-Type-Options: nosniff
Referrer-Policy: same-origin
Vary: Origin
Similar topic I found: Difference between static STATIC_URL and STATIC_ROOT on Django
and this: Django + React The resource was blocked due to MIME type (“text/html”) mismatch (X-Content-Type-Options: nosniff)
But messing with python manage.py collectstatic resolved nothing.
I have this lines of code to manage static files:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join('/app/label_it/front/static/frontend/')
And this is how I start my uwsgi connection:
python manage.py collectstatic --no-input
uwsgi --http 0.0.0.0:8080 --master --enable-threads --module label_it.wsgi
Beside collectstatic I've tried messing with <script src"static_file_path"></script> this static path with:
<script src="../../static/frontend/main.js"></script>
# and
<script src="{% static "frontend/main.js" %}"></script>
Which result in nothing.
Any clues appreciated !
EDIT:
My file structure:
Ok I just did some testing with my own server and django project to confirm my suspicion and I've come to the following conclusion: This error will happen if the path to the static file doesn't exist. Meaning that uwsgi cannot find this main.js file with the settings you have.
My suggestion would be to do the following to ensure your static files are being served correctly in a way that django knows where to look.
By default django searches inside each installed app for a static folder to find static assets. Because your project is not setup that way you have to specify additional directories where static files live.
This is what STATICFILES_DIRS is for.
In your settings.py put the following
STATICFILES_DIRS = [
BASE_DIR / 'label_it/front/static'
]
This will make all files inside label_it/front/static discoverable to django.
Now this means that
<script src="{% static "frontend/main.js" %}"></script>
will (hopefully) work.
additionally, remember to include your static urls in your urls.py file if you haven't already.
urls.py
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = [
...
]
# static content urls
urlpatterns += staticfiles_urlpatterns()
Sidenote: STATIC_ROOT is intended to be the location where all static files will be collected to when you run python manage.py collectstatic.
As such it should be an empty directory, preferably in the root of your django project.
STATIC_ROOT = os.path.join(BASE_DIR, 'static_collected')
The name of the folder doesn't really matter and I've used static_collected as an example.
If you run the command hopefully you'll see all the files in label_it/front/static/ copied there.

Some static files in django serve 404 when other dont- any ideas why?

Using django 1.5
I got static files configured like this:
STATIC_ROOT = '/home/<user>/Projects/<name>/static'
STATIC_URL = '/static/'
i just run manage.py collectstatic
directory listing:
static/css
static/css/bootstrap.css
static/css/addressbook.css
static/css/bootstrap-responsive.css
static/css/rewrite.css
static/css/login.css
when I type
localhost:8000/static/css/addressbook.css i got 404
but:
localhost:8000/static/css/bootstrap.css
gives me a proper css content
WTF? they are in the same folder and have same user/rights/groups
part from menage.py runserver output:
[24/Jul/2013 12:18:19] "GET /static/css/addressbook.css HTTP/1.1" 404 1663
[24/Jul/2013 12:19:16] "GET /static/css/login.css HTTP/1.1" 200 533
[24/Jul/2013 12:20:12] "GET /static/css/addressbook.css HTTP/1.1" 404 1663
[24/Jul/2013 12:32:51] "GET /static/css/bootstrap.css HTTP/1.1" 304 0
UPDATE:
It's serving files not from "project/static" but from static folder under application folder
I figure that out by deleting static forder under one app - files start to give 404. Same if I disable AppDirectoriesFinder.
But it still not consistent some applications don't serve files even from "static" under application folder.
My ideal situation will be: AppDirectoriesFinder commented out and all files served from myProject/static/
You can't just drop files into the /static folder of your project. Django doesn't "see" files in that directory unless they were collected from the apps via collectstatic. You will get a 404 trying to access files that you drop in there manually.
You need to put static files into the /static directory of an app (if it doesn't belong in a specific app, just create a new one, for example "main" or "website").
Alternatively you can set the STATICFILES_DIRS for Django to search additional directories when you call collectstatic.
I had the same problem. Here's my solution:
Add this to your settings.py
STATICFILES_FINDERS = (
"django.contrib.staticfiles.finders.FileSystemFinder",
#"django.contrib.staticfiles.finders.AppDirectoriesFinder"
)
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)

404 not found assets files in django

I don't know why I'm getting this error continuously modifying the routes of my settings.py, I have the tree directory of my project as follows:
/dgp
/assets
/css
/js
...
/sales
manage.py
/dgp
settings.py
As you can see, I want to access the assets folder which is one folder up to settings.py, in my settings files I have the follow configuration:
PROJECT_ROOT = os.path.dirname(os.path.realpath(__file__))
STATIC_ROOT = os.path.abspath(os.path.join(PROJECT_ROOT, '..','static'))
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.abspath(os.path.join(PROJECT_ROOT,"..","assets")),
)
But always I'm getting a 404 error finding assets files:
"GET /static/assets/css/bootstrap.min.css HTTP/1.1" 404 1688
I don't know why it's concatenating /static with /assets but I prove hard-coding with absolute pahts and neither... I don't know what's wrong... any ideas?
How are you referencing the static files in your templates? It looks like you are including assets/ in that path when you shouldn't be.
For example, if you are using {% static "assets/css/bootstrap.min.css" %} try switching it to {% static "css/bootstrap.min.css" %}. If you are using the {{ STATIC_URL }} variable within the templates to get the path, you would also just drop the assets/ part of the path in the same way.

How do I serve static admin files in Django 1.4?

I have a Django project that was started with Django 1.2. Now I'm trying to run it under Django 1.4, in a development environment, using the built-in webserver. One thing I cannot get working is the static files for the admin interface. In my django server's window, I see:
Django version 1.4, using settings 'settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[18/Jul/2012 11:38:04] "GET /admin/ HTTP/1.1" 200 6452
[18/Jul/2012 11:38:05] "GET /admin/admin/css/base.css HTTP/1.1" 404 4249
[18/Jul/2012 11:38:05] "GET /admin/admin/css/dashboard.css HTTP/1.1" 404 4264
I see that ADMIN_MEDIA_PREFIX has been deprecated, but I'm clearly missing the HOWTO that tells me what I have to do to get the admin pages working in development.
Have you tried running collectstatic to gather your static files?
https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#collectstatic
I finally started another project with "django-admin startproject", and started picking through the settings file. I found that the new settings file had:
added 'django.contrib.staticfiles' to INSTALLED_APPS
added a few STATIC_ settings. I copied these four:
STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = ()
STATICFILES_FINDERS = ()
And now things appear to be working.
In django 1.4 you can use the static tag
{% load static %}
<a href="{% static 'img/foo.png' %}">

Django admin media not loading

I'm trying to deploy this application with nginx/gunicorn but I don't know why the admin media files are missing.
settings.py:
ADMIN_MEDIA_PREFIX = '/srv/www/antingprojects.com.ar/gobras/static/admin/'
I also tried:
ADMIN_MEDIA_PREFIX = '/static/admin/'
project folder:
/srv/www/antingprojects.com.ar/gobras/static/admin/css|js|img
urls.py:
(r'^static/admin/(?P<path>.*)$', 'django.views.static.serve')
nginx access.log:
"GET /admin/ HTTP/1.1" 200 1556 "-"
"GET /srv/www/antingprojects.com.ar/gobras/static/admin/css/base.css HTTP/1.1" 404 1136 "http://antingprojects.com.ar/admin/"
"GET /srv/www/antingprojects.com.ar/gobras/static/admin/css/dashboard.css HTTP/1.1" 404 1141 "http://antingprojects.com.ar/admin/"
"GET /admin_media/img/admin/nav-bg.gif HTTP/1.1" 404 1114
You need to set up a symbolic link from the directory where the admin media files are to the static media directory you are using to serve your static files.
The admin media files should be at something like:
path/to/django/contrib/admin/media/
Once your symlink is set up you would set your ADMIN_MEDIA_PREFIX to the symbolic directory within the directory where you serve your static files, so something like this should work:
ADMIN_MEDIA_PREFIX = '/static/admin/'
Check to see where your admin is trying to load its media files from, and this should help you get started if you can't figure out which directory to use.
Here's a quick tutorial on symlinks:
http://ubuntuforums.org/showthread.php?t=255573
Your ADMIN_MEDIA_PREFIX is correct, you just need to remove /admin portion from your static view URL, because what you have now specified translates to a URI beginning with /static/admin/admin for accessing your admin resources through the static view.
Thank you for your help, the problem was in the nginx.conf, I wasn't locating /static folder.
location /static {
root /srv/www/antingprojects.com.ar/gobras;
}