django-html-static

在学习django教程的时候,想要在模板中引入css,js等静态文件,鼓捣了有一天多,终于在stackoverflow找到教程,现开始贴代码,记录一下.

首先在根目录下新建一个staticfiles文件,里面存放需要的各种css,js等文件.

然后设置setting.py:


PROJECT_DIR = os.path.dirname(file)
MEDIA_ROOT = os.path.join(PROJECT_DIR, ‘media’)

MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static')

STATIC_URL = '/static/'

STATICFILES_DIRS = (
os.path.join(PROJECT_DIR, 'staticfiles'),
)

url.py:


from django.conf.urls import patterns, include, url

from django.conf.urls.static import static

from django.contrib.staticfiles.urls import staticfiles_urlpatterns

from mysite import settings

admin.autodiscover()

urlpatterns = patterns('',('^$', hello),

url(r'^dance/$', dance),

) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

urlpatterns += staticfiles_urlpatterns()

templates文件夹下的模板文件:

模板

新手上路,一起交流

Chaos Soong wechat
你敢扫试试? 试试就试试!