如何将django中的单个对象导入到html模板中



我正在用django创建一个网站,其中有两个模型,1:Gifi(包含.gif图像(和2:categorite!当我点击其中一个.gif图像时,我想被发送到另一个html模板,在那里显示该图像和有关它的信息。我已经做了一些编码,当我点击该图像时,可以到达html页面,但问题是,除了url上的id外,django中没有任何数据被导入到该html页面。我知道这个问题很简单,但我是新手,我不知道代码。

这是型号:

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>

它已经过了几天,所以你可能已经得到了答案,但Django的视图函数和模板非常简单,你可以写:models.py:

from django.db import models
class Categorite(models.Model):
# id field is already exists models.Model not necessary
#id = models.AutoField(primary_key=True) 
name = models.CharField(max_length=100)

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)
category = models.ForeignKey(Categorite, on_delete=models.CASCADE, related_name='gifis')

views.py:

def category(request, id):
# For the foreign key fields _id field auto created also
objs = Gifi.objects.filter(category_id=id)  
return render(request, 'website/category.html', {'gifis': objs})

def code(request, id):
obj = get_object_or_404(Gifi, pk=id)
return render(request, 'website/code.html', {'gifi': obj})

和模板website/category.html

{% for gifi in gifis %}
<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 %}

和模板website/code.html

<img src="/static/{{gifi.foto}}" id="foto" alt="" >
<p>{{gifi.emri}}</p>

为了满足PEP8规范,建议使用under_score作为变量名,并建议使用CamelCase作为类名。如果你是Django的新手,我强烈推荐官方教程页面会有很大帮助。

最新更新