Hopefully, Y'all can help me with this one and hopefully I am in the right areas to post this. Just recently began learning Django and I am trying to get it deployed to Linux. I have been using gunicorn and nginx for this deployment and for the most part I have been successful. I am able to successfully deploy and interact with my app, navigate pages and interact with some of the post requests. The only thing I have been banging my head around trying to figure out what is going on with my static files. All of my css and images do not display currently and I have tried searching everywhere for the resolution. I have tried using an alias in the nginx file and I have made sure that my static root and URL is fine, nothing i have tried has done the trick. the weird this is, when looking at the access logs from nginx it shows the Get request going to the correct file and path, but shows 404? I am at a loss here lol! What is really weird is that the static folder contains a few csv's that are processed and served by a view those work correctly so I am confident that the url and root in setting.py is correct. Some of the other things I have tried include setting STATIC_URL to '/' and '/Static_File_Storage' neither worked one gave me an error about media url and the other gave me a permission denied error. This is also for a network-only site and won't be accessed by anyone other than a few employees so I am not worried about having it pulling from home.
**NGINX FILE:**
server {
listen 80;
server_name redacted;
location = /favicon.ico { access_log off; log_not_found off; }
location /Static_File_Storage/ {
alias /home/beachhouse/PycharProjects/BH_Django_Project/BH_DJANGO/Static_File_Storage/;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}}
**setting.py**
STATIC_ROOT = os.path.join(BASE_DIR, "/Static_File_Storage/")
STATIC_URL = 'home/beachhouse/PycharmProjects/BH_Django_Project/BH_DJANGO/Static_File_Storage/'
**Actual Location of File:** /home/beachhouse/PycharmProjects/BH_Django_Project/BH_DJANGO/Static_File_Storage/css/style.css
**output on logs: "GET** /home/beachhouse/PycharmProjects/BH_Django_Project/BH_DJANGO/Static_File_Storage/home/beachhouse/PycharmProjects/BH_Django_Project/BH_DJANGO_Static_File_Storage/css/style.css HTTP/1.1" 404 1226
**Static Tags in HTML:** {% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'css/style.css' %}"/>
<img class="login_img" src="{% static '/logo.png' %}" alt="logo">
View call for static: df = pandas.read_csv(static('Flash_Changes.csv'))
Change STATIC_URL to "/Static_File_Storage/".
Related
I know there are already several threads on the topic. I've been through most of them (especially all the troubleshooting listed in this one) but I can't figure out my issue.
I am trying to use a Bootstrap template in my Django project, and I'd like to simply start by accessing the files in the /static/ directory. My project directory looks like this :
Whenever I try to load the page http://localhost:8000/static/theme/assets/css/style.css it returns a Page not found error (and obviously no CSS/JS content appears on my index).
Here are my settings:
I have debug = True
ÌNSTALLED_APPS contains django.contrib.staticfiles
settings.py looks like this :
STATIC_URL = "/static/"
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static/"),)
But I still can't access anything from the /static/ directory.
I tried to access the CSS and JS files in base.html this way :
{% load static %}
...
<link href="{% static 'theme/assets/css/style.css' %}" rel="stylesheet">
I really have no clue how I could solve this.
Thanks in advance for your help !
Is base.html properly bound to a URL and to a view function via a URL dispatcher ? Can you access that file from your browser ?
If yes, try to substitute this line
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static/"),)
with this one
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),)
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">
I have some javascript that I would like to run on a particular page of a Django site I am building. In the template I currently have:
<script type='text/javascript' src='{% static "/home_page/github_repos.js" %}'></script>
The HTML that is generated by this is:
http://localhost:8000/home_page/github_repos.js
The problem is that I have the javascript in /static/home_page/.
If I hardcode in the path as:
<script type='text/javascript' src='static/home_page/github_repos.js'></script>
Everything works fine.
However, I am getting odd behavior that I do not understand:
If, in the template I set the path like this:
<script type='text/javascript' src='{% static "static/home_page/github_repos.js" %}'></script>
The HTML generate has a src attribute of /static/static/home_page/github_repos.js
The part that is really throwing me off is that if I do not put /static/ at the front of the src when using the template tags (like the first example I give), /static/ is not added, however, if I do add /static/ at the front, it gets added. I need /static/ added to the front of the src attribute, but only once.
Some relevant information:
My home_page app resolves to '/' as the url, so localhost:8000/ gets you to the home page.
I have included'django.contrib.staticfiles' in my installed apps
Finally, STATIC_URL = '/static/'
Is this problem occurring because my home_page app resolves to the root address of localhost:8000/?
Any help is appreciated.
This issue is because the path in the static tag should be relative to the static directory. From the docs:
Uses the configured STATICFILES_STORAGE storage to create the full URL for the given relative path
You're using an absolute path. Instead, you should use:
{% static "home_page/github_repos.js" %}
Note the omission of the leading forward slash.
On my local server, I'm getting the following error in terminal
[03/Oct/2011 22:49:19] "GET /favicon.ico/ HTTP/1.1" 500 65893
In my site_base.html, I already have the following line:
<link rel="icon" href="{{ STATIC_URL }}images/favicon.ico" />
What's causing this error and how can I make it stop?
The default lookup browsers do is /favicon.ico. However, since you specified the link the browser shouldn't be doing this. There's most likely a bug in the particular browser and version you're using responsible for sending the extra request, but that's beside the point.
You're getting a 500 error because that particular request is making it into the Django URL handling machinery, and whatever view is responding to, is choking on the provided arguments.
Check your urls.py and see which pattern(s) will accept the URL /favicon.ico/. Then, go into the corresponding views and see why it's causing it to choke, or alter the pattern such that it won't catch a URL like this, as it mostly likely shouldn't be in the first place.
This may not be 100% the issue that you're facing, but it's worth mentioning since you're using a custom favicon stored in your static folder.
In Django templating, you can manage your static assets like this:
{% load static %}
<link rel="shortcut icon" href="{% static 'img/favicon.ico' %}">
No need to send a {{ STATIC_URL }} context variable into the template.
In my case, the problem was appearing when I was trying to log in on the admin panel.
My error was:
ValueError: unsupported format character 'C' (0x43) at index 34
Internal Server Error: /favicon.ico
The problem was nested in my urls.py:
Suggestions from other users help me a little but in the end, the documentation was a game changer. Quote:
from django.contrib.staticfiles.storage import staticfiles_storage # for `favicon` -> https://simpleit.rocks/python/django/django-favicon-adding/
from . import views
urlpatterns = [
path('favicon.ico', RedirectView.as_view(url=staticfiles_storage.url('project_foo\logos\media\favicon.ico')))]
I mean line: path('favicon.ico', RedirectView.as_view(url=staticfiles_storage.url('project_foo\logos\media\favicon.ico')))]
As the documentation stated quote: django.contrib.staticfiles collects static files from each of your applications (and any other places you specify) into a single location that can easily be served in production.
In my case, I pointed out a file, not the location. Things were ok on the user page, but throw an error on the admin panel.
The solution in my case is:
`path('favicon.ico', RedirectView.as_view(url=staticfiles_storage.url('project_foo\logos\media')))]`
I'm using apache+mod_wsgi for django.
And all css/js/images are served through nginx.
For some odd reason, when others/friends/colleagues try accessing the site, jquery/css is not getting loaded for them, hence the page looks jumbled up.
My html files use code like this -
<link rel="stylesheet" type="text/css" href="http://x.x.x.x:8000/css/custom.css"/>
<script type="text/javascript" src="http://1x.x.x.x:8000/js/custom.js"></script>
My nginx configuration in sites-available is like this -
server {
listen 8000;
server_name localhost;
access_log /var/log/nginx/aa8000.access.log;
error_log /var/log/nginx/aa8000.error.log;
location / {
index index.html index.htm;
}
location /static/ {
autoindex on;
root /opt/aa/webroot/;
}
}
There is a directory /opt/aa/webroot/static/ which have corresponding css & js directories.
The odd thing is that the pages show fine when I access them.
I have cleared my cache/etc, but the page loads fine for me, from various browsers.
Also, I don't see 404 any error in the nginx log files.
Any pointers would be great.
I think using root in location block is incorrect. I use alias and it works fine, even without re-configuring django.
# django settings.py
MEDIA_URL = '/static/'
# nginx server config
server {
...
location /static {
autoindex on;
alias /opt/aa/webroot/;
}
}
Hope this makes things simpler.
server_name must match hostname in link/script URLs. Either declare your configuration as default for this interface:port pair (listen 8000 default)
Nginx must listen on the interface where your host's IP is bound (seems ok in your case)
I also struggled with this. However, following trick worked for me:
server {
listen 8000;
server_name localhost;
access_log /var/log/nginx/aa8000.access.log;
error_log /var/log/nginx/aa8000.error.log;
location / {
index index.html index.htm;
}
location ^/static/ {
autoindex on;
root /opt/aa/webroot/;
}
}
I just marked static as a regex with ^ and nginx started serving static files. No modification on Django side was needed.
MEDIA_URL shall not be used to serve the Static content like js etc. Django provides a separate STATIC_URL settings option that can be used.
So this can be changed as
<script type="text/javascript" src="{{STATIC_URL}}js/jquery-1.3.2.min.js"></script>
Also, its more standard to use staticfile app templatetag like this:
{% load static from staticfiles %}
<script type="text/javascript" src="{% static 'js/jquery-1.3.2.min.js' %}"></script>
Docs Here
Fim & Alexander - Thanks for the hints those helped.
Here is how I solved it for anyone stuck in the same boat -
settings.py -
>MEDIA_ROOT = ''
MEDIA_URL = 'http://x.x.x.x:8000/static/'
In my html -
<script type="text/javascript" src="{{MEDIA_URL}}js/jquery-1.3.2.min.js"></script>
In my views.py -
return render_to_response('templates/login-register.html', {},
context_instance=RequestContext(request));
nginx inside the sites-available config file -
listen x.x.x.x:8000;
server_name x.x.x.x.;
Restarted nginx
Restarted apache