django statics

8 views
Skip to first unread message

Kakar Arunachal Service

unread,
Jul 14, 2013, 6:05:31 PM7/14/13
to django...@googlegroups.com
Hello,
I read the django docs, but my still confused. How do i use the static files, for css?
Thank you.

Sébastien Billion

unread,
Jul 14, 2013, 7:28:24 PM7/14/13
to django...@googlegroups.com
Hi,

In your project folder, you can create a folder media zith a subfolder css, an other img, js, whatever.
In your settings.py, you can use this line to get the absolute path dynamically:

from os import path
PROJECT_ROOT = path.dirname(path.abspath(__file__))

After, you need to specify your MEDIA_ROOT and MEDIA_URL


MEDIA_ROOT = path.join(PROJECT_ROOT,'media')

MEDIA_URL = '/media/'

In your template, if you want to get your css or your js, img, etc... you just need to do 
<link href="/media/css/style.css" rel="stylesheet">

Regards,
Seb


2013/7/15 Kakar Arunachal Service <kakararuna...@gmail.com>
Hello,
I read the django docs, but my still confused. How do i use the static files, for css?
Thank you.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Phang Mulianto

unread,
Jul 15, 2013, 5:54:24 AM7/15/13
to django-users
Hi,

Better not use hard coded url path for static, use {{ STATIC_URL }} in your link ;
eg : <img src="{{ STATIC_URL }}/image/logo.jpg">


Tomas Ehrlich

unread,
Jul 15, 2013, 7:36:28 AM7/15/13
to django...@googlegroups.com
Hi there,
first, there's a difference between MEDIA files and STATIC files (since
1.3, I think). Media files are files uploaded by users, while static
files are usually persistent files on the server from the beginning,
eg. stylesheets, images, javascripts, robots.txt, etc.

It's good to keep these files separated, since static files are usualy
in VCS, while media files not.


As others said, it's convinient to use {{ MEDIA_URL }} and
{{ STATIC_URL }} variables in your templates. Just don't forget to use
RequestContext with proper context processor while rendering template:
https://docs.djangoproject.com/en/dev/ref/templates/api/#django-core-context-processors-static


Django should't server static/media files in production. They should be
served using your http server (nginx, apache, ...). In development,
however, you can add static files handler to your urls:

# your_project/urls.py
from django.conf.urls import patterns, include, url

urlpatterns = patterns('',
... # your url patterns
)

# Serve static and media files in development
from django.conf import settings
if settings.DEBUG:
from django.conf.urls.static import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
media = static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns = (media + staticfiles_urlpatterns() + urlpatterns)


Cheers,
Tom


Dne Mon, 15 Jul 2013 17:54:24 +0800
Phang Mulianto <brav...@gmail.com> napsal(a):
Reply all
Reply to author
Forward
0 new messages