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



>想象一下,你得到了一个图像提要,一旦你点击一张图片,它就会在新模板中显示它,有点像评论视图,图片在顶部,评论部分在下面。

我唯一实现的是我在新选项卡中显示了单击的图像:

<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>

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


所以我把它想象成这样:

  • 创建一个锚链接,将此单击的图像URL传递到新视图(如上所示:"image.body.url"(
  • 在新模板中,获取此 URL 并将其显示为图像


希望我能用底线描述它。提前:)感谢

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

感谢@pasta1020的回答,我做了以下工作:


模板1.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})


模板2.html

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

最新更新