Serving static file with django - django

I am new to Django and am trying to learn how to serve static files in local deployment. I have read this and many other possible related problems from stackoverflow but i cant seem to be able to find solve my problem.
This is my settings , I cant figure out what is the reason Django isnt serving my static files
STATIC_ROOT = ''
STATIC_URL = '/html/'
STATICFILES_DIRS=('C:/Users/RayLim/Desktop/project/home/username/djcode/mysite/mysite/templates/html',)
In my templates I refer to them as
<link rel="stylesheet" href="css/style.css" type="text/css" media="all" />
The structure of my html file which is located at C:/Users/RayLim/Desktop/project/home/username/djcode/mysite/mysite/templates
html
/css
/images
/style.css
/js
/jquery.jcarousel.pack
/jquery-1.4.1.min
/jquery-func
This is the server response
"GET /home/css/images/big1.jpg HTTP 1.1" 404 3782

First, static files and templates are usually in separate directories but I don't think there would be any problem (that I'm aware of...)
Next, you want to make sure that you use a template tag to represent the static file location instead of using css/.. which expects there to be a css directory relative to whatever url you are currently viewing.
There are a couple template tags you can use, I use {{ STATIC_URL }}
So replace
<link rel="stylesheet" href="css/style.css" type="text/css" media="all" />
with
<link rel="stylesheet" href="{{ STATIC_URL }}css/style.css" type="text/css" media="all" />
And you should be all set...

You should avoid hard linking to your static directories. It can cause confusion and potential problems as your project gets bigger
Please refer to this, it has everything you need.
Django staticfiles app help

Related

Django not loading static files into assets file

I recently started a basic django project, i want to load an already made default template. This template has an assets folder with all the CSS, JS files etc
This folder, is later called in the templates, so for example i can have:
<base href="../">
// And a lot of static files being called like this:
<link href="./assets/scrollbar/css/scrollbar.css" rel="stylesheet" type="text/css" />
<link href="./assets/somecss.css" rel="stylesheet" type="text/css" />
// And so on..
<script src="./assets/somejsfile.js" type="text/javascript"></script>
The problem with this is that none of the files in the assets are being retrieved. I know that the problem depends on where i put the assets folder, but i don't know how to solve that. I tried to add it to different parts of my project's structure but it doesn't work, since i only get a lot of errors like this:
http://127.0.0.1:8000/assets/somecss.css net::ERR_ABORTED 404 (Not Found)
Here is my settings.py:
STATIC_URL = '/static/'
In this static folder, there is a basic css file i tested before trying this.
And here is the structure of my project:
//MAIN FOLDER
migrations
ASSETS
templates -> INDEX.HTML (where the assets folder is called)
views, forms etc
Have you read the documentation on this?
It is recommended to use the static template tag to make the filesystem location of your static files independent from your URLs. Using this tag, your URLs would have to be defined like this:
{% load static %}
<link href="{% static 'assets/scrollbar/css/scrollbar.css' %}"
rel="stylesheet" type="text/css" />
<link href="{% static 'assets/somecss.css' %}"
rel="stylesheet" type="text/css" />
You also need to define the setting STATICFILES_DIRS because it looks like your static assets are not inside a default location inside an app of your project.

Django Compressor : cache messing up

I have a webapp using django-compressor for js, css and less minification.
I'm using COMPRESS_OFFLINE = True because I'm using django compressor to build a less file containing import to other files (otherwise django compressor does not rebuild files if make change in core.less):
// base.html
{% compress css %}
<link rel="stylesheet" type="text/less" media="all" href="{% static 'less/main.less' %}" />
{% endcompress %}
// main.less
#import "core.less";
#import "variables.less";
#import "utils.less";
#import "sidebar.less";
I am running into the following issue: i have the following .css files in assets/CACHE/css:
2601cbccb2ae.css
52a7aa59f552.css
729b9866970c.css
They are all corresponding to a modification of my core.less file. The issue is that when I log in my webapp, it seems Django-Compress {%compress%} use all those file and not only the last one. So sometimes I have the good design, and if I refresh I got the old one...
// First time the page is ok:
<link href="/static/CACHE/css/2601cbccb2ae.css" media="all" rel="stylesheet" type="text/css"/>
// After reloading I got the old design
<link href="/static/CACHE/css/52a7aa59f552.css" media="all" rel="stylesheet" type="text/css"/>
Each time I refresh the file changes... So I assume there is something related to django compressor cache but I have really no idea how to solve this...
If you guys have an idea, feel free to help me.

Django Static Files - 404

I know there has been a lot of questions on this, but none of them seemed to help me at all. Here's the scenario.
I have a website using the Django Web Framework on IIS on Windows I've been working on. In order to deploy static files, I've been using the collectstatic functionality of the framework to collect the static files. This aspect of it works fine. From my settings.py:
STATIC_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), 'static'))
# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'
# Additional locations of static files
STATICFILES_DIRS = (
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
'C:/work/wincrash/dev/dev/analysis/static',
)
Running collect static successfully pulls all my static files from C:/work/wincrash/dev/dev/analysis/static and puts them in the /static/ folder in the root directory of the site.
My problem occurs when I'm trying to load the static files on the webpage. Here's a snippet from my base.html page that loads on every page. None of the following static files are loading, failing with the 404 error:
<link type="text/css" href="{{ STATIC_URL }}media/css/custom-theme/jquery-ui-1.8.22.custom.css" rel="Stylesheet" />
<script type="text/javascript" src="{{ STATIC_URL }}media/js/jquery/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="{{ STATIC_URL }}media/js/jquery/jquery-ui-1.8.22.custom.min.js"></script>
<!-- Base css sheet -->
<link rel="stylesheet" href="{{ STATIC_URL }}media/css/base.css" type="text/css" media="all" />
Which ends up like this when the HTML is rendered:
<link type="text/css" href="/static/media/css/custom-theme/jquery-ui-1.8.22.custom.css" rel="Stylesheet">
<script type="text/javascript" src="/static/media/js/jquery/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="/static/media/js/jquery/jquery-ui-1.8.22.custom.min.js"></script>
<!-- Base css sheet -->
<link rel="stylesheet" href="/static/media/css/base.css" type="text/css" media="all">
So I guess what I'm asking is what is preventing these files from being loaded? Is it the Django Framework? Is it IIS? The IIS logs are showing the 404 not found for all the static files. Why is this? How does IIS know where to look, and how can I help point it in the right direction?
Thanks for all in advance for your help. I know this might seem like a duplicate question but all the other questions I found on here weren't much help. I've been at this for a while and just would like to move past it.
I figured it out.
I guess you need a web.config file in your static folder in order for IIS to find the static folder from which to service the static files. When I put a web.config file with the following content in the static folder where I had all the static files, it worked immediately.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<handlers>
<!--
This removes Helicon Zoo handler and makes IIS processing static files.
-->
<remove name="django.project#x64" />
<remove name="django.project#x86" />
</handlers>
</system.webServer>
</configuration>
try adding {% load staticfiles %} to your template
https://docs.djangoproject.com/en/dev/howto/static-files/#configuring-static-files

Favicon using static url in Django

I'm linking the favicon to my site like so:
<link rel="icon" type="image/png" href="{{ STATIC_URL }}favicon.png" />
but it still doesn't show up. I've seen a lot of ways using a static uri, but I'd like to just keep the Django way of utilizing STATIC_URL.
You write STATTIC_URL with two "Ts"
<link rel="icon" type="image/png" href="{{ STATIC_URL }}favicon.png" />
But if you write "STATIC_URL" is possible that is not work.The favicon is very caching, access with private mode.

css pulling from the wrong location with {{ STATIC_URL }} on my form page only

I'm using django 1.3
I'm using a base.html template which my pages inherit from. Everything works fine except for one page (which has a form on it).
That page is trying to call the CSS from /links/addlink/css/site.css but this is the wrong location. It is in /static/css/site.css
In my base.html I have <link rel="stylesheet" href="{{ STATIC_URL }}css/site.css" type="text/css" charset="utf-8" />
If I create a completely seperate template and use the full path like <link rel="stylesheet" href="http://127.0.0.1:8000/static/css/site.css" type="text/css" charset="utf-8" /> then my page renders correctly.
What is wrong?
You are not using RequestContextin your view.
See here: Referring to static files in templates