django imagefield中图像的相对url映射
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不是你的 urlconf 的功能,而是你如何在模板中显示图像的功能。你可能正在做这样的事情:
当你应该做的时候
......注意最初的斜杠。或者,如果您使用 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:
when you should be doing
... 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.