Django 开发服务器:从自定义视图渲染的模板未获取静态媒体

发布于 2024-09-26 04:28:59 字数 1177 浏览 1 评论 0原文

我有一个使用内置 django 服务器运行(并提供内容)的开发服务器。我从通用视图渲染的所有模板都正确指向静态媒体文件(css/java/imgs),但通过自定义视图渲染的模板似乎没有将 /media/ 文件夹添加到 url 中。 (至少这似乎是问题)

在我的设置中我有:

DJANGO_PATH = os.path.realpath(os.path.dirname(__file__))
DB_PATH = os.path.join( (os.path.split(DJANGO_PATH))[0] , 'db/dev.db')
TEMPLATE_PATH = os.path.join( DJANGO_PATH , 'templates')
DEBUG = True
TEMPLATE_DEBUG = DEBUG

MEDIA_PATH = os.path.join( (os.path.split(DJANGO_PATH))[0] , 'media')
ADMIN_MEDIA_PREFIX = '/media/admin/'
MEDIA_URL = '/media/'
MEDIA_ROOT = MEDIA_PATH

并且在我的网址中我有一个条目

(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes': True }),

有人有任何想法吗?

编辑:
哎呀,忘记说了。我的所有模板都继承自一个基本模板,该模板包含所有媒体文件,例如:

{{ MEDIA_URL }}css/some/file.css

因此,在我的模板文件夹中,我有:

/templates/base.html
/templates/someapp/childtemplate.html

标头中的所有 css/js 都如上面所示。然后,在特定于我的应用程序的模板中,我只需继承基本模板

此外
我可以通过访问来查看媒体

localhost:8000/media/  

,没有问题,所以 urlCONF 似乎正在完成它的工作

I have a development server running (and serving content) using the built in django server. All my templates that are rendered from generic views point correctly to the static media files (css/java/imgs) but ones that are rendered via custom views don't seem to prepend the /media/ folder to the urls. (At least this seems to be the problem)

In my settings I have:

DJANGO_PATH = os.path.realpath(os.path.dirname(__file__))
DB_PATH = os.path.join( (os.path.split(DJANGO_PATH))[0] , 'db/dev.db')
TEMPLATE_PATH = os.path.join( DJANGO_PATH , 'templates')
DEBUG = True
TEMPLATE_DEBUG = DEBUG

MEDIA_PATH = os.path.join( (os.path.split(DJANGO_PATH))[0] , 'media')
ADMIN_MEDIA_PREFIX = '/media/admin/'
MEDIA_URL = '/media/'
MEDIA_ROOT = MEDIA_PATH

and In my urls I have an entry

(r'^media/(?P<path>.*)

Anyone got any ideas?

EDIT:
Ooops, forgot to mention. All my templates inherit from a base template which has all the media files like:

{{ MEDIA_URL }}css/some/file.css

So in my templates folder I have:

/templates/base.html
/templates/someapp/childtemplate.html

with all the css/js in the header like above. Then in the templates specific to my applicaiton I am simply inheriting the base template

Furthermore
I can view the media by visiting

localhost:8000/media/  

no problem, so the urlCONF seems to be doing it's job

, 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes': True }),

Anyone got any ideas?

EDIT:
Ooops, forgot to mention. All my templates inherit from a base template which has all the media files like:

So in my templates folder I have:

with all the css/js in the header like above. Then in the templates specific to my applicaiton I am simply inheriting the base template

Furthermore
I can view the media by visiting

no problem, so the urlCONF seems to be doing it's job

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月朦胧 2024-10-03 04:29:00

确保 django.core.context_processors.media 位于 settings.py 中的 TEMPLATE_CONTEXT_PROCESSORS 变量中。这样就可以在模板中使用 {{ MEDIA_URL }}

编辑:正如 Daniel 指出的,我忘了提及,是的,为了利用您的 TEMPLATE_CONTEXT_PROCESSORS ,您需要将 context_instance=RequestContext(request) 传递给 render_to_response 函数在您的视图中。必须对每个视图执行此操作的一些替代方法是使用 django-annoying 第三方应用程序中的 render_to() 装饰器,或者仅导入并使用 direct_to_template 而不是 render_to_response,这是一个巧妙的技巧,因为它会自动使用 RequestContext

Make sure that django.core.context_processors.media is in your TEMPLATE_CONTEXT_PROCESSORS variable in settings.py. This enables the use of {{ MEDIA_URL }} in your templates.

Edit: As Daniel pointed out, I forgot to mention that yes, in order to take advantage of your TEMPLATE_CONTEXT_PROCESSORS you need to pass context_instance=RequestContext(request) to the render_to_response function in your view. Some alternatives to having to do that for every view are to use the render_to() decorator in the django-annoying third-party app, or to just import and use direct_to_template instead of render_to_response, which is a neat trick in that it automatically uses the RequestContext.

初心 2024-10-03 04:29:00

如果我没记错的话,默认情况下无法从模板上下文访问 MEDIA_URL 。
最简单的解决方法是创建一个新的模板上下文处理器:

def media_url(request):
    from django.conf import settings
    return {'media_url': settings.MEDIA_URL} 

settings.py 中:

TEMPLATE_CONTEXT_PROCESSORS = ('myapp.context_processors.media_url',)

只需记住在渲染模板时使用 RequestContext 即可。在视图中:

from django.template import RequestContext
return render_to_response("my_app/my_template.html", {'some_var': 'foo'}, context_instance=RequestContext(request))

您的模板现在应该正确呈现。

有关此内容的更多信息:
http://www.b- list.org/weblog/2006/jun/14/django-tips-template-context-processors/

If I'm not mistaken, MEDIA_URL is not accessible by default from the template context.
The easiest workaround is to create a new template context processor:

def media_url(request):
    from django.conf import settings
    return {'media_url': settings.MEDIA_URL} 

And in settings.py:

TEMPLATE_CONTEXT_PROCESSORS = ('myapp.context_processors.media_url',)

Just remember to use the RequestContext when rendering the template. In the view:

from django.template import RequestContext
return render_to_response("my_app/my_template.html", {'some_var': 'foo'}, context_instance=RequestContext(request))

Your templates should now render correctly.

For more on this:
http://www.b-list.org/weblog/2006/jun/14/django-tips-template-context-processors/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文