0

我是初学者,但我一直在到处寻找解决方案。我看不到上传的图片 (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/。需要明确的是,当我只使用通用视图时,效果很好。但是有了视图,我看不到图像(图像链接很好)。静态文件效果很好。所以这是我的代码:

网址.py

from mysite.cms.views import superpages
urlpatterns = patterns('',
(r'^(?P<url>.*)$', superpages),)

视图.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))
4

1 回答 1

1

您的 urls.py 有问题。我想你已经像这样定义了你的模式:

urlpatterns = patterns('',
    (r'^(?P<url>.*)$', superpages),
    (r'^media/(?P<path>.*)$',
     'django.views.static.serve',
     {'document_root': settings.MEDIA_ROOT}),
)

A URL such as http://192.168.1.1:8000/media/portfolio/icon.png/ matches the first pattern so your superpages view is called and raises a 404. What you need to do is put your catch-all superpages pattern at the very end of your urlpatterns. Or you can choose a different approach with a middleware, see what django.contrib.flatpage does for an example.

于 2011-08-24T18:37:41.497 回答