我正在用django创建一个网站,我有两个模型,1:Gifi(包含.gif图像)和2:categate!当我单击其中一个.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>
这几天过去了,所以您可能已经得到了答案,但是Django的视图函数和模板非常简单,您可以编写: 该
models.py
:以及{}:
和模板
website/category.html
和模板
website/code.html
为了满足PEP 8规范,建议变量名使用under_score,类名使用CamelCase。 如果你是Django的新手,我强烈推荐official tutorial页面会有很大帮助
相关问题 更多 >
编程相关推荐