django imagefield中图像的相对url映射

发布于 2024-11-14 16:08:35 字数 1019 浏览 3 评论 0原文

我正在使用 imagefield 来渲染图像。这些是网址;

    (r'^site_media/(?P<path>.*)$', 'django.views.static.serve',
    {'document_root': site_media}),
    (r'^$', 'webapp.blog.views.index'),
    url(
        r'^blog/view/(?P<slug>[^\.]+).html', 
        'webapp.blog.views.view_post', 
        name='view_blog_post'),

这些是视图:

def index(request):
    return render_to_response('index.html', {
        'categories': Category.objects.all(),
        'posts': Blog.objects.all()[:5]
    })

def view_post(request, slug):   
    return render_to_response('view_post.html', {
        'post': get_object_or_404(Blog, slug=slug),
    })

如果我使用 {{ posts.photos.url }} 调用索引页面中的图像对象,它会正确映射到 http://127.0.0.1/site_media/images /image.jpg。但是,如果我在 view_post 模板上调用图像对象,它将映射到 http://127.0.0.1/blog/view/site_media/images/image.jpg。如何使 view_post 函数将图像网址映射到正确的 http://127.0.0.1/site_media/images/image.jpg 网址。

I am using imagefield to render images. These are the urls;

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

These are the views:

def index(request):
    return render_to_response('index.html', {
        'categories': Category.objects.all(),
        'posts': Blog.objects.all()[:5]
    })

def view_post(request, slug):   
    return render_to_response('view_post.html', {
        'post': get_object_or_404(Blog, slug=slug),
    })

If I call a image object in the index page with {{ posts.photos.url }} it properly maps to http://127.0.0.1/site_media/images/image.jpg. But if I call the image object on view_post template it gets mapped to http://127.0.0.1/blog/view/site_media/images/image.jpg. How can I make view_post function map image urls to the proper http://127.0.0.1/site_media/images/image.jpg url.

, 'django.views.static.serve', {'document_root': site_media}), (r'^

These are the views:


If I call a image object in the index page with {{ posts.photos.url }} it properly maps to http://127.0.0.1/site_media/images/image.jpg. But if I call the image object on view_post template it gets mapped to http://127.0.0.1/blog/view/site_media/images/image.jpg. How can I make view_post function map image urls to the proper http://127.0.0.1/site_media/images/image.jpg url.

, 'webapp.blog.views.index'), url( r'^blog/view/(?P<slug>[^\.]+).html', 'webapp.blog.views.view_post', name='view_blog_post'),

These are the views:

If I call a image object in the index page with {{ posts.photos.url }} it properly maps to http://127.0.0.1/site_media/images/image.jpg. But if I call the image object on view_post template it gets mapped to http://127.0.0.1/blog/view/site_media/images/image.jpg. How can I make view_post function map image urls to the proper http://127.0.0.1/site_media/images/image.jpg url.

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

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

发布评论

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

评论(1

维持三分热 2024-11-21 16:08:35

这不是你的 urlconf 的功能,而是你如何在模板中显示图像的功能。你可能正在做这样的事情:

<img src="site_media/images/image.jpg">

当你应该做的时候

<img src="/site_media/images/image.jpg">

......注意最初的斜杠。或者,如果您使用 STATIC_URL 或 MEDIA_URL,请确保它们在 settings.py 中使用初始斜杠进行定义。

This is not a function of your urlconf, but how you are displaying the images in your template. You are probably doing something like this:

<img src="site_media/images/image.jpg">

when you should be doing

<img src="/site_media/images/image.jpg">

... note the initial slash. Alternatively, if you're using STATIC_URL or MEDIA_URL, make sure they're defined with the initial slash in your settings.py.

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