【发布时间】:2020-12-19 11:00:04
【问题描述】:
我正在使用 django 创建一个网站,其中有 2 个模型,1:Gifi(包含 .gif 图像)和 2:categorite!当我单击其中一个 .gif 图像时,我希望将其发送到另一个 html 模板,在该模板中显示该图像和有关它的信息。我已经完成了一些编码,当我单击图像时,我进入了 html 页面,但问题是没有来自 django 的数据被导入到该 html 页面,除了 url 上的 id。我知道问题很简单,但我是新手,我不知道代码。
这是模型:
from django.db import models
class categorite(models.Model):
name = models.CharField(max_length=100)
id = models.AutoField(primary_key=True)
class Gifi(models.Model):
foto = models.ImageField(upload_to='website/static/')
emri = models.CharField(max_length=100)
Source = models.CharField(max_length=100)
Kodet = models.CharField(max_length=12)
categoryId = models.ForeignKey(categorite, on_delete=models.CASCADE)
id = models.AutoField(primary_key=True)
这是views.py:
from django.shortcuts import render,get_object_or_404
from .models import Gifi,categorite
# Create your views here.
def home(request):
return render(request, 'website/home.html')
def categories(request):
content = {
'view': categorite.objects.all()
}
return render(request, 'website/categories.html',content)
def PostaCode(request):
return render(request, 'website/PostaCode.html')
def Contact(request):
return render(request, 'website/Contact.html')
def category(request,id):
content = {
'view': Gifi.objects.filter(categoryId_id=id),
}
return render(request, 'website/category.html',content)
def code(request,id):
content = {
'view': get_object_or_404(Gifi,pk=id)
}
return render(request, 'website/code.html',content)
这是我点击图片的模板:
{% for gifi in view %}
<a href="{% url 'code' gifi.id %}" class="gif">
<img src="/static/{{gifi.foto}}" id="foto" alt="" >
<p id="source">Source: {{gifi.Source}}</p>
<p id="permbatjaa">Coding: {{gifi.Kodet}}</p>
</a>
{% endfor %}
这是我需要到达的模板,以及关于图像的信息应该在哪里(code.html):
<img src="/static/{{gifi.foto}}" id="foto" alt="" >
<p>{{gifi.emri}}</p>
【问题讨论】:
标签: python html django web django-queryset