django-photologue not loading CSS - django

I have setup django-photologue, and I am trying to get it to load the default templates. I can get the HTML file loaded, but it won't load any of the CSS (mainly just bootstrap)
When accessing photologue, I get the following error on the console:
Not Found: /photologue/gallery/css/bootstrap.min.css
[24/Aug/2018 13:42:52] "GET /photologue/gallery/css/bootstrap.min.css HTTP/1.1" 404 7311
This is odd to me, because I am almost certain that the css file is present.
This is the django code including the CSS file (taken from the photologue example project):
<link href="{{ STATIC_URL }}css/bootstrap.min.css" rel="stylesheet" media="screen">
The resultant HTML is:
<link href="css/bootstrap.min.css" rel="stylesheet" media="screen">
No matter where I put the CSS file, I get a 404 error.
I have photologue templates in myapp/templates/photologue, because for whatever reason that's what worked.
The HTML I have there works fine, but the CSS just won't load. Not in it's own subfolder in the photologue templates directory, not as a subfolder in the myapp tempaltes directory, not as standalone files, not when I put them in the static folder....I've tried putting them in every possible location and reloading the site, and it makes no difference.
What can I do to make my template, which loads correctly, load and see CSS files?
edit: settings.py : https://pastebin.com/kRj21j3m

The issue here is that your server is looking for your css file here:
/photologue/gallery/css/bootstrap.min.css
Where it doesn't actually exist, hence the 404 error (file/page not found).
If you're using the base Django instructions for setting up a project, it is highly likely that your static files are found here:
/static/css/bootstrap.min.css.
Please make sure you have the following in your settings.py file:
STATIC_ROOT = os.path.join(BASE_DIR, 'public', 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
I also suspect that you may want this in your base.html file (or whichever template you're extending from):
{% load static %}
And then this in your template's head:
<link href="{% static 'css/bootstrap.min.css' %}" rel="stylesheet" media="screen">

Related

Django 4 Static files : what's the best practice

I'm currently building a project on Django 4.0 and I want to do the static files management the best and the cleaner for this version.
Currently I have this project tree :
And here is my settings file :
BASE_DIR = Path(__file__).resolve().parent.parent.parent
(...)
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
When I try to find some videos about the subject, no one is using the same structure and setup for static files. In this sample, I have a 404 error on my dist/css/output.css file.
In my HTML template I try to call it that way :
<link href='{% static "css/dist/output.css" %}' type="text/css" rel="stylesheet">
Could someone please copy/past me an easy static setup for handling static properly ?
Or at least, help me to understand why it doesn't work and what I should do ?
Moreover, I put my static directory outside my main app, but some are putting it in. So I don't know what's best...
Thanks :)
The best way to configure your static files in django 4 is to use pathlib instead of importing an extra module eg. os
STATICFILES_DIRS = [
BASE_DIR / "static",
]
and problem is that you don't have css folder inside your static folder so instead of this
<link href='{% static "css/dist/output.css" %}' type="text/css" rel="stylesheet">
you have to put this
<link href='{% static "dist/output.css" %}' type="text/css" rel="stylesheet">
or you can create css folder and add dist folder inside it

Static files not loaded into templates

I am new to Django and so far all I know about static files is that they are CSS, JS, and images and they should be in a directory called static within the app directory
but when I use one of these files in my template like that:
first I load the static files
{% load static %} <!-- in the 1st line of the template -->
then I link the CSS file like that
<link href="{% static 'auctions/styles.css' %}" rel="stylesheet">
They don't load and the styles don't seem to appear
so I just want to know what I am missing here
this is the project tree enter image description here
static root and url from settings
STATIC_URL = '/static/'
STATIC_ROOT = 'E:/Work/SoftwareDevelopment/Web/Django/commerce/auctions/static'
Did you ran python manage.py collectstatic?
You also need to configurate your settings.py with STATIC_URL = '/static/'
Docs
Are your auctions's static folder separated in css, js, images folders? If so, you are missing specifying that in static:
'auctions/css/styles.css'
If that doesn't work, try running collectstatic manually and getting the path to the file from there to pass in the html.

Docker Django can't find static files 404error

I would like to deploy my website in django to docker. Problem is, when i run docker-compose up, website html is loaded, but static content like css, js, logo, mp3 not. I think that this is because my url. On localhost my website runs correctly, but server has prefix something like http://127.0.0.1:8000/Something. I repaired urls so html files run but my static files in settings.py looks like this:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static")
]
and for example my homepage.html with js and css included (this works on localhost) like this:
<head>
<meta charset="UTF-8">
<title>Title</title>
{% load static %}
<link rel="stylesheet" href="{% static "styles/homepage.css" %}">
<script src="{% static "scripts/homepage.js" %}"></script>
</head>
but when server runs on http://127.0.0.1:8000/Something it show up this error in the command line:
As you can see, the path must be not http://86.110.225.19/static/styles/homepage.css but
http://86.110.225.19/SOMETHING/static/styles/homepage.css
How can i fix this? Thank you so much.
Change the configuration to:
STATIC_URL = '/Something/static/'
I find out, what made this error. I added to settings.py line:
SUB_SITE = '/Something/'
and it works! :)

Static File Whitenoise Heroku Django issue

I have an API developed in Django and Django Rest Framework. We need one page in "normal" Django that will be open once a month maybe (so no need for CDN for the static files). Gunicorn + whitenoise is what we went ahead with.
The collectstatic works fine in both build phase and after build phase.
The url generated on the page is href=/static/css/edit_card.a1c6e0f9f12e.css/ but the console shows the 404 not found for that resource and there are no styles applied to the page.
Relevant django settings:
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.10/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static_media/')
STATICFILES_DIRS = [
os.path.join(BASE_DIR + "/static_folder/"),
]
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
The relevant file in the repo is in /static_folder/css/edit_card.css
The relevant file on the heroku instance after running collectstatic is in
static_media/css/edit_card.a1c6e0f9f12e.css (together with the normal version and other compressed files)
I can manually access this link url/static/css/edit_card.css which is ridiculously weird.
This works fine when DEBUG = True. When in False/production it does not.
Could someone point me in the right direction? Thanks.
EDIT:
Template
{% load static %}
<link rel="stylesheet" type="text/css" href={% static "css/edit_card.css" %}/>
It might be too late for this response, but I'm surprised nobody noticed the error.
Your template is as follows:
<link rel="stylesheet" type="text/css" href={% static "css/edit_card.css" %}/>
The problem is that the href attribute is not quoted and it's taking the last / as part of the path. That's why the URL is: href=/static/css/edit_card.a1c6e0f9f12e.css/ (note the / at the end).
The solution would be:
<link rel="stylesheet" type="text/css" href="{% static "css/edit_card.css" %}" />
URL is between quotes and there's a space after the path.

static files in pybb

I have some problems with static file, when i use them in pybb. I work on django 1.3 and python 2.6.5.
My settings file is here
I put my css and images in /static/pybb/css and /static/pybb/images
when i put tag like
<link rel="stylesheet" href="{{ STATIC_URL }}pybb/my.css">
in my templates, it doesn't work. This is the same with image
<img src="{{ STATIC_URL }}pybb/images/myimage.jpg">
To sum it up, my own static files don't appear in my project templates.
any idea? perhaps i forgot something
Why are you putting css in package static files directory?
Also directories in STATICFILES_DIRS should not overlaps. Look at example provided with pybbm or even start project from this example.