您的位置:首页 > 编程语言 > Go语言

Django:解决media、static和template路径问题

2014-07-20 10:28 435 查看
将html文件加入到django路径有以下三步操作:

1、配置template路径

template路径用于存储.html文件

TEMPLATE_DIRS = (
os.path.join(os.path.dirname(__file__), '..', 'templates').replace('\\','/'),
os.path.join('templates'),
)
2、配置static路径

static路径用于存储.css .js .txt等静态文件

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/
STATIC_URL = '/static/'

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(os.path.dirname(__file__), '..', 'static').replace('\\','/'),
os.path.join('static'),
)

配置static路径的绝对路径
# 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 = os.path.join(PROJECT_ROOT, 'static')

3、配置media路径

media路径用于存储.jpg .mp4等用户上传的文件

import os
PROJECT_ROOT = os.path.realpath(os.path.dirname(__file__))

# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: "/home/media/media.lawrence.com/media/"
MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'media')

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://media.lawrence.com/media/", "http://example.com/media/"
MEDIA_URL = '/media/'

为了能够访问media路径,需要在url中设置
from django.conf import settings
from django.conf.urls.static import static

if settings.DEBUG:
# static files (images, css, javascript, etc.)
urlpatterns += patterns('',
(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT}))
注意:url(r'^','views.action')会导致media无法访问,因为没有加$截止url
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息