Django RequestContext 和媒体不起作用
我是初学者,但我一直在到处寻找解决方案。我看不到上传的图片 (404)。
图像链接错误(例如:http://192.168.1.1:8000/media/portfolio/icon.png/ -> 顺便说一句,这个正确的 url ):
No SuperPages matches the given query.
SuperPages 是我的模型,其中包含 url 对象。
我为媒体文件配置了所有内容,如下所示: http ://www.muhuk.com/2009/05/serving-static-media-in-django-development-server/。需要明确的是,当我仅使用通用视图时,它效果很好。但对于视图,我看不到图像(图像链接很好)。静态文件效果很好。这是我的代码:
urls.py
from mysite.cms.views import superpages
urlpatterns = patterns('',
(r'^(?P<url>.*)$', superpages),)
views.py
from django.template import loader, RequestContext
from mysite.cms.models import SuperPages
from django.shortcuts import get_object_or_404
from django.http import HttpResponse, HttpResponseRedirect
DEFAULT_TEMPLATE = 'default.html'
def superpages(request, url):
if not url.endswith('/') and settings.APPEND_SLASH:
return HttpResponseRedirect("%s/" % request.path)
if not url.startswith('/'):
url = "/" + url
f = get_object_or_404(SuperPages, url__exact = url)
t = loader.get_template(DEFAULT_TEMPLATE)
c = RequestContext(request, {
'superpages': f,
})
return HttpResponse(t.render(c))
I'm beginner, but I've been looking everywhere for solution. I can't see uploaded images (404).
Error from image link (for example:http://192.168.1.1:8000/media/portfolio/icon.png/ -> by the way, this proper url ) :
No SuperPages matches the given query.
SuperPages is my model which contains url object.
I configured everything for media files like here: http://www.muhuk.com/2009/05/serving-static-media-in-django-development-server/. And to be clear, when I'm using generic views only, it works great. But with views, I can't see images (links to images are fine). Static files works great. So this is my code:
urls.py
from mysite.cms.views import superpages
urlpatterns = patterns('',
(r'^(?P<url>.*)
views.py
from django.template import loader, RequestContext
from mysite.cms.models import SuperPages
from django.shortcuts import get_object_or_404
from django.http import HttpResponse, HttpResponseRedirect
DEFAULT_TEMPLATE = 'default.html'
def superpages(request, url):
if not url.endswith('/') and settings.APPEND_SLASH:
return HttpResponseRedirect("%s/" % request.path)
if not url.startswith('/'):
url = "/" + url
f = get_object_or_404(SuperPages, url__exact = url)
t = loader.get_template(DEFAULT_TEMPLATE)
c = RequestContext(request, {
'superpages': f,
})
return HttpResponse(t.render(c))
, superpages),)
views.py
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的 urls.py 有问题。我想您已经这样定义了您的模式:
诸如
http://192.168.1.1:8000/media/portfolio/icon.png/
之类的 URL 与 first 模式匹配因此,您的superpages
视图被调用并引发 404。您需要做的是将您的 catch-allsuperpages
模式放在 urlpatterns 的最末尾。或者您可以选择使用中间件的不同方法,请参阅 django.contrib.flatpage 的示例。There's something wrong with your urls.py. I suppose you have defined your patterns like this:
A URL such as
http://192.168.1.1:8000/media/portfolio/icon.png/
matches the first pattern so yoursuperpages
view is called and raises a 404. What you need to do is put your catch-allsuperpages
pattern at the very end of your urlpatterns. Or you can choose a different approach with a middleware, see whatdjango.contrib.flatpage
does for an example.