Django管理员:类媒体 [英] Django Admin : Class Media
问题描述
我有一个类媒体如下
class helloAdmin(admin.ModelAdmin):
class Media:
js = ['choice.js']
admin.site.register(hello,helloAdmin)
staticfiles应用程序将STATIC_URL添加到媒体路径。但是我想要将MEDIA_URL替换为STATIC_URL,而STATIC_URL不为空。如何做到这一点?
The staticfiles app prepends STATIC_URL to the media path. But I want MEDIA_URL to be prepended instead of STATIC_URL and STATIC_URL isn't empty. How can this be done?
推荐答案
您可以通过以下设置实现:
You can achieve that with the following settings:
import os
CURRENT_PATH = os.path.abspath((os.path.dirname(__file__)))
MEDIA_ROOT = os.path.join(CURRENT_PATH, 'uploads')
MEDIA_URL = '/site_media/'
urls.py
urls.py
from django.conf import settings
urlpatterns += patterns('',
url(r'^site_media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
)
admin.py
admin.py
class helloAdmin(admin.ModelAdmin):
class Media:
js = ['/site_media/choice.js']
admin.site.register(hello,helloAdmin)
使用 MEDIA_URL
不建议为静态文件提供服务。根据django文档, MEDIA_URL
不会被引用用于媒体文件,当 STATIC_URL
not empty 。请参阅这部分文档
Using MEDIA_URL
is not advised for serving static files. According to django documentation, MEDIA_URL
will not be referred for media files, when STATIC_URL
is not empty. Refer to this part of documentation.
所以我建议您使用以下设置:
So I recommend you to use the following settings:
STATIC_ROOT = 'static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(CURRENT_PATH, 'media'),
)
admin.py
admin.py
class helloAdmin(admin.ModelAdmin):
class Media:
js = ['choice.js']
admin.site.register(hello,helloAdmin)
这篇关于Django管理员:类媒体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!