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
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
相关文章推荐
- 有时候,一点小问题,就会纠结你半天,真是基础不牢啊。 彻底解决 django的模板templates路径的问题; 一般会提示什么模板找不到,类似:TemplateDoesNotExist at /ac
- Windows下Apache+Django+mod_wsgi的static和media问题处理
- Django中static、media与template设置
- django template 路径问题
- Django ====> 1.4版本静态文件路径设置--MEDIA_ROOT,MEDIA_URL, STATIC_ROOT, STATIC_URL学习
- Django1.7解决模板路径TEMPLATE_DIRS配置问题
- Django中程序中图片资源的路径问题(static文件夹的放置)
- Tango with Django笔记——template、static、media
- django+httpd+mod_pythonurl存在中文路径问题解决
- django1.4测试环境 css样式,图片路径问题解决
- 解决nginx+uWSGI部署Django时遇到的static文件404的问题
- Django static media template目录设置方法
- Django ====> 1.4版本静态文件路径设置--MEDIA_ROOT,MEDIA_URL, STATIC_ROOT, STATIC_URL学习
- django DRF图片路径问题的解决方法
- 路径问题 解决 ${pageContext.request.contextPath}
- 如何解决Java路径问题?
- 怎样解决:对路径""的访问被拒绝 这个问题?
- asp.net2.0解决相对路径出错的问题,ResolveUrl的用法
- VC中关于路径中含有空格问题的解决办法
- IIS路径禁止POST问题之解决