質問

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>.*)$', 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))
役に立ちましたか?

解決

There's something wrong with your urls.py. I suppose you have defined your patterns like this:

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top