Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup static files in Django [duplicate]

Tags:

static

django

Possible Duplicate:
How to organize and load CSS within Django project/app?

I have follow several websites about how to setup static files in Django. Here are my steps.

Configure the setting.py:

STATIC_ROOT = '/Users/kin/mysite/static'
STATIC_URL = '/static/'

Run the collectstatic command:

python manage.py collectstatic

After that, I saw my image file is copied to the STATIC_ROOT.

Then in my template, I try to use my image file by:

<img border="0" src="{{ STATIC_URL }}images.jpg" width="304" height="228">

But there is no image when I load it on the browser. I checked the page source and STATIC_URL seems empty.

Can anyone shed some light here?

Thanks

like image 962
Kintarō Avatar asked Jan 22 '13 07:01

Kintarō


People also ask

How to add files to a static folder in Django?

One can add your files (pdf, images, text files, or anything you want) in the static folder. now you need to make Django know that you have created a static folder so now add this line in settings.py file,

What is static_URL and staticfiles_dirs in Django?

STATIC_URL is the URL location of static files located in STATIC_ROOT STATICFILES_DIRS tells Django where to look for static files in a Django project, such as a top-level static folder STATIC_ROOT is the folder location of static files when collecstatic is run

How do I add a static base to a Django template?

There are two main ways to structure templates in a Django project as outlined in this tutorial. Let's assume we are using a templates/base.html file for a Blog project. To add our static base.css file to it we'd start by adding {% load static %} at the top of the file and then use the {% static %} tag with the path to it.

What is static_root in Django?

STATIC_ROOT = os.path.join (BASE_DIR, 'staticfiles') STATIC_ROOT is the single root directory from where the Django application will serve the static files in production. While deployment you would typically want to serve the static files from the conventional /var/www/example.com


3 Answers

Dont hard code the path in your setting. I usually put my static files in my main project, so my setting file look like this

import os
MAIN_PROJECT = os.path.dirname(__file__)

then

# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"
STATIC_ROOT = ''
# 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.
    os.path.join(MAIN_PROJECT, 'static/'),
)

After that, you can use {{ STATIC_URL }} in your view

like image 82
Thai Tran Avatar answered Nov 15 '22 04:11

Thai Tran


Actually I got it right by just using the following code:

{% load staticfiles %}
<img border="0" src="{% static 'image.jpg' %}" width="304" height="228">

This will render the STATIC_URL path to me.

Thanks all for helping me out!

like image 40
Kintarō Avatar answered Nov 15 '22 03:11

Kintarō


If you're running your app with python manage.py runserver then you may need to add the following to the end of your url conf (see docs).

from django.contrib.staticfiles.urls import staticfiles_urlpatterns

urlpatterns += staticfiles_urlpatterns()

The process for serving static files with django's dev server is different to the process for doing it with a production web server.

like image 38
Aidan Ewen Avatar answered Nov 15 '22 03:11

Aidan Ewen