Django 1.4在开发服务器上提供MEDIA_URL和STATIC_URL文件 [英] Django 1.4 serving MEDIA_URL and STATIC_URL files on development server
问题描述
我只是试图在开发服务器上提供静态和上传的所有文件。我的STATIC_URL文件工作,经过很多实验,但我根本无法获得MEDIA_URL文件的服务。
设置:
DIRNAME = os.path.dirname(__ file__ )
MEDIA_ROOT = os.path.join(DIRNAME,'media /')
MEDIA_URL ='/ media /'
STATIC_ROOT =''
STATIC_URL ='/ static /
STATICFILES_DIRS =()
我已经添加了媒体和静态上下文处理器: / p>
TEMPLATE_CONTEXT_PROCESSORS =(
django.contrib.auth.context_processors.auth,
django.core .context_processors.debug,
django.core.context_processors.i18n,
'django.core.context_processors.media',
'django.core.context_processors.static',
django.core.context_processors.request,
'satchmo_store.shop.context_processors.settings',
'django.contrib.messages.context_processors.messages',
)
我已经添加到url confs:
#在DEV中提供静态和上传的文件
urlpatterns + = staticfiles_urlpatterns()
urlpatterns + static(settings.MEDIA_URL,document_root = settings.MEDIA_ROOT)
在文档中添加了两个conf设置,第一个为静态,第二个为媒体。
我的结构,网站是一个应用程序,静态目录放置在其中在djangoproject上指示:
< myproject>
pre>
--media
--settings
--templates
--website
| - > static
在模板中,我可以提供静态内容没有问题
{{STATIC_URL}} css / style.css
但是使用photologue的任何上传的图像都是没有提供,但网址是正确的:
/media/photologue/photos/cache/spawning-2_admin_thumbnail.jpg
该目录结构在媒体/
,超级困惑现在看起来好像很复杂,而我从来没有遇到过任何问题。
解决方案我对Django和我很新静态内容从未有过任何问题。
这是我的配置。我希望它可以帮助
我的文件夹结构
django-project
--mainapp
----settings.py
----wsgi.py
---- [...]
--otherapp
--fixtures
--static
--templates
--manage.py
--requirements.txt
settings.py
import os,socket
DEBUG = True
TEMPLATE_DEBUG = DEBUG
MAIN_APP = os.path.abspath(os.path.dirname(__ file__) )
PROJECT_ROOT = os.path.abspath(os.path.join(MAIN_APP,..))
MY_LOCALHOST =VirusVault.local#这是我本地机器的真实名称:)
尝试:HOST_NAME = socket.gethostname()
除了:HOST_NAME =localhost
[...]
如果HOST_NAME == MY_LOCALHOST:
STATIC_ROOT = os。 path.join(PROJECT_ROOT,'static /')
STATIC_URL =/ static /
MEDIA_ROOT = os.path.join(STATIC_ROOT,'media /' )
MEDIA_URL =/ media /
else:
STATIC_ROOT =/ server / path / to / static / files
STATIC_URL =http://server.com / static /
MEDIA_ROOT =/ server / path / to / static / files / media /
MEDIA_URL ='http://server.com/static/media/'
您需要将
'django.contrib.staticfiles'
添加到INSTALLED_APPS
urls.py
if settings.HOST_NAME == settings.MY_LOCALHOST:
urlpatterns + = patterns('',
(r'^ media /(?P< path>。*)$','django.views.static。服务',
{'document_root':settings.MEDIA_ROOT,'show_indexes':True}),
)
urlpatterns + = patterns('',
(r'^ static /(?P<path>.*)$','django.views.static.serve',
{'document_root':settings.STATIC_ROOT,'show_indexes':True}),
)
Just upgraded to Django 1.4, and having serious trouble with the new 'improved' serving of static and media files on development server. I love Django, but why on earth they have made serving these files doubly more complicated with STATIC_URL,STATIC_ROOT, STATICFILES_DIR now is utterly beyond me.
I'm simply trying to serve all files, static and uploaded, on the development server. I have the STATIC_URL files working, after much experimentation, but I simply cannot get the MEDIA_URL files to be served as well.
Settings:
DIRNAME = os.path.dirname(__file__) MEDIA_ROOT = os.path.join(DIRNAME, 'media/') MEDIA_URL = '/media/' STATIC_ROOT = '' STATIC_URL = '/static/' STATICFILES_DIRS = ()
I've got the media and static context processors added:
TEMPLATE_CONTEXT_PROCESSORS = ( "django.contrib.auth.context_processors.auth", "django.core.context_processors.debug", "django.core.context_processors.i18n", 'django.core.context_processors.media', 'django.core.context_processors.static', "django.core.context_processors.request", 'satchmo_store.shop.context_processors.settings', 'django.contrib.messages.context_processors.messages',
)
and I've added in the url confs:
# serve static and uploaded files in DEV urlpatterns += staticfiles_urlpatterns() urlpatterns + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
with the two conf settings added as indicated in the docs, first one for static, second for media.
my structure, website being an app and static dir placed inside it as instructed on djangoproject:
<myproject> --media --settings --templates --website |->static
In templates I can serve static content no problem with
{{STATIC_URL}}css/style.css
But any uploaded image, this one using photologue, is not served, but the urls are correct:
/media/photologue/photos/cache/spawning-2_admin_thumbnail.jpg
That directory structure does exit under media/
Super, super confused. It all seems so ridiculously complicated now, whereas I never had any issues before.
解决方案I´m very new to Django and I´ve never had any problem with static content.
This is my configuration. I hope it can help
My folder structure
django-project --mainapp ----settings.py ----wsgi.py ----[...] --otherapp --fixtures --static --templates --manage.py --requirements.txt
settings.py
import os, socket DEBUG = True TEMPLATE_DEBUG = DEBUG MAIN_APP = os.path.abspath(os.path.dirname(__file__)) PROJECT_ROOT = os.path.abspath(os.path.join(MAIN_APP, "..")) MY_LOCALHOST = "VirusVault.local" # this is the real name of my local machine :) try: HOST_NAME = socket.gethostname() except: HOST_NAME = "localhost"
[...]
if HOST_NAME == MY_LOCALHOST: STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static/') STATIC_URL = "/static/" MEDIA_ROOT = os.path.join(STATIC_ROOT, 'media/') MEDIA_URL = "/media/" else: STATIC_ROOT = "/server/path/to/static/files" STATIC_URL = "http://server.com/static/" MEDIA_ROOT = "/server/path/to/static/files/media/" MEDIA_URL = 'http://server.com/static/media/'
You need to add
'django.contrib.staticfiles'
to INSTALLED_APPSurls.py
if settings.HOST_NAME == settings.MY_LOCALHOST: urlpatterns += patterns('', (r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes': True}), ) urlpatterns += patterns('', (r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT, 'show_indexes': True}), )
这篇关于Django 1.4在开发服务器上提供MEDIA_URL和STATIC_URL文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!