How can I change the Django Admin Static Files url? - django

I'm hosting my Django project on my own server, and exactly as the docs say, the Django admin media stop showing up.
The solution is simply to host it yourself, which I'm doing. The problem I'm having is that the url the Django admin is using to try to find them is incorrect. Specifically, Django is looking at
<link rel="stylesheet" type="text/css" href="/ceasarb-cfa/admin/css/base.css">
when I want it to look at
<link rel="stylesheet" type="text/css" href="/ceasarb-cfa/static/admin/css/base.css">
My question is, how can I change that path?
Intuitively, I've tried adjust the ADMIN_MEDIA_PREFIX file in settings.py (currently set to /ceasarb-cfa/static/admin) but fiddling with that value didn't seem to change anything.

My guess is that your settomgs.py you have:
STATIC_URL = "/ceasarb-cfa/"
but it should be
STATIC_URL = "/ceasarb-cfa/static/".
Here's more documentation on that setting:
https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-STATIC_URL

Related

django-photologue not loading CSS

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">

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.

PyCharm3, how to get rid of "cannot resolve directory" messages?

I am using PyCharm3 for developing and I get "Cannot Resolve Directory" errors in places like these:
<link href="/static/css/styles.css" rel="stylesheet" type="text/css" />
However, if I do this:
<link href="/my_project/static/css/styles.css" rel="stylesheet" type="text/css" />
I do not get the error any more. So I guess it must be something in PyCharm's settings that I can fix. But what is that?
This is my settings.py
import os
PROJECT_DIR = os.path.dirname(__file__)
STATIC_ROOT = os.path.join(PROJECT_DIR, "staticfiles/")
STATICFILES_DIRS = (
os.path.join(PROJECT_DIR, "static"),
)
I also tried to set static/ as "sources" folder.
PS: This is not a django error. It is a PyCharm error. My web application is working fine.
I know it has been asked a while ago. I faced the same issue with Intellij with Python Plugin similar to Pycharm.
Fix is as follows.
Mark the template folder as Template source.
Then you can add the template language as django
in language and
frameworks Ctrl+Shift+S
See the following details for reference
Use this
{% load staticfiles %}
<img src="{% static "my_app/myexample.jpg" %}" alt="My image"/>
as per docs found here: https://docs.djangoproject.com/en/1.6/howto/static-files/
I was having the same problem. What I've done to fix this was:
I've enabled Django support on Pycharm = Settings -> Languages & Framework -> Django

Get the absolute static file URL in my Django template

I want to know how can I get the absolute URL of my static file directly in my template in Django ?
For now in my template :
<link rel="stylesheet" href="{% static "css/bootstrap.min.css" %}">
return
<link rel="stylesheet" href="/static/css/bootstrap.min.css">
How can I get on dev:
<link rel="stylesheet" href="http://127.0.0.1:8000/static/css/bootstrap.min.css">
on production
<link rel="stylesheet" href="https://mycompany.com/static/css/bootstrap.min.css">
There are two options:
recommended: use the sites framework to render the appropriate domain
not recommended: store your current domain as a Django setting in the settings file you use depending on your environment
I usually go for (1), the only downside being that you have to update the current domain in the DB, but that usually happens just once per deployment.
Then the appropriate domain will be displayed irrelevant of where code is running; you should always use the static tag in your template, rather than handling the display of the domain manually.