如何在django的新视图中显示点击的项目/图像?

时间:2018-06-06 20:00:15

标签: html django django-templates django-views click

想象一下,您有一个图像供稿,一旦您点击一个图像,它就会在新模板中显示它,有点像评论视图,其中图片位于顶部,评论部分位于下方。

我唯一取得的成就是我在新标签中显示了点击后的图片:

<a href="{{image.body.url}}" target="_blank"> <img style="width:250px; background-color:yellow;flex-shrink: 0;min-width: 100%;min-height: 100%" src="{{ image.body.url }}" /> </a>

这不是我想要的......:/


所以我想象它是这样的:

  • 制作一个锚点链接,将点击后的图片网址传递给新视图(如上图所示:“image.body.url”)
  • 在新模板中获取此网址并将其显示为图片


希望我描述得不好。 Thx提前:))

2 个答案:

答案 0 :(得分:1)

您的问题有点难以理解,可能是您的图片/图片网址来自数据库?如果是,则将图像的主键传递到新视图,以便可以在新模板中显示它。

答案 1 :(得分:0)

感谢@ pasta1020的回答,我做了以下内容:


template1.html

<a href="{% url 'detail' pk=image.pk %}" >
   <div style="width:250px;height:250px;" >
      <img  style="width:250px; background-color:yellow;" src="{{ image.body.url }}" />
   </div>
</a>


urls.py

url(r'^home/detail/(?P<pk>\d+)/$',views.detail,name='detail'),


views.py

def detail(request,pk):
    imagePk = Image.objects.get(pk=pk)
    imagePkUrl = imageP.body.url

    return render(request, 'template2.html', {'theSrc': imagePkUrl})


template2.html

<img  style="width:250px;height: 250px;background-color:yellow;" src="{{theSrc}}"/>