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

来源:互联网 发布:shtml nginx 编辑:程序博客网 时间:2024/06/06 19:40

将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 osPROJECT_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 settingsfrom django.conf.urls.static import staticif 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

0 0
原创粉丝点击