【问题标题】:How do i import single object from django to a html template如何将单个对象从 django 导入到 html 模板
【发布时间】: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


    【解决方案1】:

    几天过去了,你可能已经得到答案了,但是 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>
    

    为符合PEP 8 规范,建议变量名使用under_score,类名建议使用CamelCase。 如果您是 Django 新手,我强烈推荐 official tutorial 页面会很有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-13
      • 2017-04-26
      • 2016-09-28
      • 2014-10-07
      • 2021-04-18
      • 2020-02-17
      • 1970-01-01
      相关资源
      最近更新 更多