How to server Static Files with Django 1.4 on Windows? - django

I need to link a CSS file to a simple hmtl page and it just doesn't work! I have red the Docs and watched like 10 Videos that develop on Linux Systems, but non of them come along with the Config of Windows!
for some reason the version I'm using creates the Tree in this way! (correct me if am wrong, because am really new to Django)
mysite
\blog
\static
style.css
\templates
index.html
__init__.py
models.py
tests.py
views.py
\mysite
url.py
settings.py
wsgi.py
manage.py
I added the url to settings.py file:
STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = (
('assets','blog/static'),
views.py looks like this :
from django.http import HttpResponse,Http404,HttpRequest
from django.shortcuts import render_to_response
def change_form(request):
return render_to_response('index.html')
then I did the Collectstatic command, and a Dir under the name (assets) is added to the root Directory of mysite
and my index.html looks like this
{& load static &}
<!DOCTYPE html>
<html>
<head>
<!--Meta tags-->
<meta charset="utf-8">
<!--Title-->
<title>Menu Notification Badges</title>
<!--Stylesheets-->
<link rel="stylesheet" href="{% static 'assets/css/styles.css' %}">
</head>
and I'm getting this Error now :
TemplateSyntaxError at /change-form/
Invalid block tag: 'static'
Request Method: GET
Request URL: http://localhost:8000/change-form/
Django Version: 1.4.5
Exception Type: TemplateSyntaxError
Exception Value:
Invalid block tag: 'static'
Exception Location: C:\Python27\lib\site-packages\django\template\base.py in invalid_block_tag, line 321
Python Executable: C:\Python27\python.exe
Python Version: 2.7.3
Python Path:
['E:\\DjangoSites\\mysite',
'C:\\Windows\\system32\\python27.zip',
'C:\\Python27\\DLLs',
'C:\\Python27\\lib',
'C:\\Python27\\lib\\plat-win',
'C:\\Python27\\lib\\lib-tk',
'C:\\Python27',
'C:\\Python27\\lib\\site-packages']
Server time: Sat, 27 Apr 2013 09:57:21 +0300
Error during template rendering
In template E:\DjangoSites\mysite\blog\templates\index.html, error at line 13
Invalid block tag: 'static'
3 <html>
4 <head>
5
6 <!--Meta tags-->
7 <meta charset="utf-8">
8
9 <!--Title-->
10 <title>Menu Notification Badges</title>
11
12 <!--Stylesheets-->
13 <link rel="stylesheet" href="{% static 'assets/styles.css' %}">
PLEASE DON'T Direct me to the DOCS! and NOT a Solution with LINUX file System!
THIS is the kindda things that makes me doubt why I chose to be a WebDev!

{& load static &}
should probably be
{% load static %}

I faced similar issue in windows so this is what i did to server my static page with my CSS
make a folder named media and place all your css and js file there and now do following step
settings.py
MEDIA_ROOT = "".join(os.path.join(os.path.dirname(file), 'media').replace('\','/'))
STATICFILES_DIRS = (
os.path.join(os.path.dirname(file),'static').replace('\','/'),
os.path.join(os.path.dirname(file),'media').replace('\','/'),
)
STATIC_DOC_ROOT = '/path/to/media'
urls.py
make a mapping of that folder as a url
(r'^site_media/(?P.*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT}),
HTML(in your media folder all CSS and js should be there)
script src="{{MEDIA_URL}}bootstrap/js/bootstrap.min.js">
script src="{{MEDIA_URL}}dropzone/dropzone.js">
views.py
def char(request):
t = get_template('name.html')
html = t.render(Context({'MEDIA_URL':'http://www.google.com/site_media/'}))
return HttpResponse(html)
this will solve your problem.

Related

unable to render static files in Django 2.2

I just started working on a project where the previous developer was using django 2.2
Ideally, leaving the complexity aside, one would need to define STATIC_URL='/static/' in settings.py file and use {% load 'static' %} in base html file while appending required details in urls.py file.
However, in the current project, all of the static content (css, js) is stored in single media folder.
and in settings.py file, i have
STATIC_ROOT = '/usr/local/my34project/lib/python3.4/site-packages/django/contrib/admin/static'
STATIC_URL = '/static/'
also in base html file, they have
<link href="/media/css/backoffice/fonts.css" rel="stylesheet" type="text/css">
instead of
<link href="{% static 'css/backoffice/fonts.css' %}" rel="stylesheet" type="text/css">
How is it, that the previous person's project was working without static being loaded or used in the url link ?

Cannot load static files in django

I added the following lines in my settings.py file:
STATIC_URL = '/static/'
STATICFILES_DIR = [
os.path.join(BASE_DIR,"static"),
]
STATIC_ROOT = os.path.join(BASE_DIR,"static")
Then I edited my HTML file to use {% load static %} and <img src="{% static 'static/myapp/images/ABC.png' %}" alt="">, but still I am not able to see the ABC.png pic on my webpage. Can someone please help?
Screenshots of .py files:
The issus is that you are incorrectly requesting the asset, use {% static 'myapp/images/ABC.png' %} instead. I suppose that you have the following structure in your app:
myapp
static
myapp
images
ABC.png
templetes
views.py
...
As you can see, is not necessary to add the name of your statics directory. The path is relative to the static folder of any of your apps in your project.

Static does not loads static files in Django

This is my directory structure
app_web
_init_.py
settings.py
urls.py
wsgi.py
upload_app
migrations/
static/
js/
alert.js
templates/
upload.html
_init_.py
admin.py
apps.py
models.py
tests.py
views.py
db.sqlite3
manage.py
In settings.py , my
STATIC_URL = '/static/'
and in my upload.html
{% load staticfiles %}
<script type="text/javascript" src="{% static "js/alert.js" %}"></script>
It does not works and throws 404 error everytime. I even tried load static but it still cannot load anything from static folder and throws 404 error.
I am using Windows 10 machine and Django==1.9
The idea was to create a static directory outside the upload_app folder. The reason is that in the settings.py the BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) . That means one directory above is the base directory, so it will start searching static/ from that directory. But it will never find it as I placed inside upload_app/static .
Also, I need to work on putting templates outside the app upload_app as it's not generally best practice to keep templates inside the app.
looking for other suggestions in structure

How to use a remote static file server while developing with django manage.py runserver

Background: I'm using the Django manage.py runserver for local development. I have the contrib.staticfiles in installed apps and I'm using its {% static %} template tag in my templates.
What I try to achieve: For development, I'd like to use an independent server for serving the static files but use the django development server to serve the Django app. So that I could access the page locally on my computer at http://127.0.0.1:8000, but all the static files would be served from another computer or from a different server on the localhost, as defined by the settings.STATIC_URL variable.
The problem: The settings.STATIC_URL variable is somehow overridden when I'm using the development server. So all my static files are served by the local django development server instead of what I defined in settings.STATIC_URL.
Solution: See Daniel's answer below!
settings.py:
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
STATIC_URL = os.path.join('127.0.0.1:666', BASE_DIR, 'static/')
example_template.html:
{% load staticfiles %}
{% load bootstrap3 %}
{% bootstrap_css %}
{% bootstrap_javascript jquery=True%}
{% bootstrap_messages %}
{# Load MyApp CSS #}
<link href='http://fonts.googleapis.com/css?family=Open+Sans:400,700subset=greek-ext,vietnamese,cyrillic-ext,latin-ext' rel='stylesheet' type='text/css'>
<link href="{% static 'css/my_app.main.css' %}" rel="stylesheet">
page source when using a browser:
<link href="FULL_PATH_TO_BASE_DIR_HERE/static/css/my_app.main.css" rel="stylesheet">
but expected to see:
<link href="127.0.0.1:666/FULL_PATH_TO_BASE_DIR_HERE/static/css/my_app.main.css" rel="stylesheet">
This isn't anything to do with Django or runserver. It's simply because you are using os.path.join() to join a domain and a path; that function doesn't know about URLs, and will assume that since BASE_DIR starts with a leading slash, it should normalize the whole path to start from there and ignore anything previous:
>>> os.path.join('127.0.0.1', '/foo', 'static')
'/foo/static'
The solution is twofold: don't use os.path.join on URLs, but more importantly, don't use BASE_DIR in your STATIC_URL. The filesystem directory your static files live in has nothing whatsoever to do with the URL they are exposed on. Your STATIC_URL should be something like "http://127.0.0.1:666/static/".

Managing static/images in Django with AppEngine

I am trying to add an image to my simple google app engine code, but it doesn't work if I follow the tutorial. It works if my image is in my app directory, but not when I move it to static.
When I am using it in plain html like:
<img src="../static/myimage.jpg"></img>
or
 
and many other variations, the image just does not show (it shows when it is outside of static dir). When I am doing it as in the tutorial, defining STATIC_URL in my settings file:
STATIC_URL = '/static/'
And adding this lines (or variations like "/my_image.jpg" and so on)
{% load staticfiles %}
<img src="{% static "my-app/myimage.jpg" %}" alt="My image"/>
causes server error (500). I am using django 1.3
Here is the directory structure:
my-app
\static
myimage.jpg
\templates
base.html
# and other html files
\urls.py, settings.py #and other .py files
App.yaml:
-url: /(.*\.(gif|png|jpg))
static_files: static/\1
upload: static/(.*\.(gif|png|jpg))
setting.py:
ROOT_URLCONF = 'urls'
urls.py:
STATIC_URL = '/static/'
What did you set STATIC_ROOT to?
Generally though, using GAE's static file handlers in app.yaml will give you better caching and probably lower cost than serving the images with django's staticfiles.