【问题标题】:Model data not displayed on template - django模型数据未显示在模板上 - django
【发布时间】:2020-06-21 00:40:00
【问题描述】:

短篇故事:我制作了两个应用程序。 django 项目中的属性和租户。首先,我开始将数据从属性模型渲染到 property_detail.html 模板,它工作正常,但是在我创建和迁移租户模型之后,我尝试将数据从那里渲染到 property_detail.html 它不起作用。然而它并没有给我任何错误。它只是没有显示出来。

模型.py

import arrow
import uuid
from django.db import models
from django_countries.fields import CountryField

from django.urls import reverse
from django.conf import settings
from properties.models import Property


class Tenant(models.Model):

    id = models.UUIDField(  # new
        primary_key=True,
        default=uuid.uuid4,
        editable=False)

    full_name = models.CharField("Full Name", max_length=255, null=True)
    email = models.EmailField(unique=True, null=True)
    phone = models.CharField(max_length=20, unique=True, null=True)
    description = models.TextField("Description", blank=True)
    country_of_origin = CountryField("Country of Origin", blank=True)
    creator = models.ForeignKey(
        settings.AUTH_USER_MODEL, null=True, on_delete=models.SET_NULL)

    created_on = models.DateTimeField(
        "Created on", auto_now_add=True, null=True)

    is_active = models.BooleanField(default=False)

    apartment = models.ForeignKey(
        Property,
        on_delete=models.CASCADE,
        related_name='reviews',
    )

    rent_tenant = models.CharField(
        "Rent he/she pays", max_length=10, blank=True)

    def __str__(self):
        return self.full_name

    def get_absolute_url(self):
        """"Return absolute URL to the Contact Detail page."""
        return reverse('tenant_detail', kwargs={'pk': str(self.pk)})

urls.py

from django.urls import path
from .views import TenantListView, TenantDetailView

urlpatterns = [
    path('', TenantListView.as_view(), name='tenant_list'),
    path('<uuid:pk>', TenantDetailView.as_view(), name='tenant_detail'),  # new
]

views.py

from django.views.generic import ListView, DetailView
from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMixin  # new
from .models import Tenant


class TenantListView(LoginRequiredMixin, ListView):  # new
    model = Tenant
    context_object_name = 'tenant_list'
    template_name = 'tenants/tenant_list.html'
    login_url = 'account_login'  # new


class TenantDetailView(LoginRequiredMixin, PermissionRequiredMixin, DetailView):  # new
    model = Tenant
    context_object_name = 'tenant'
    template_name = 'tenants/tenant_detail.html'
    login_url = 'account_login'  # new
    permission_required = 'books.special_status'  # new

这是我需要渲染的 html 模板部分。

<li class="list-group-item">
{% if tenant.full_name %}
<b>Layout</b> <a class="float-right">{{ tenant.full_name }}</a>
{% endif %}
</li>
<li class="list-group-item">
{% if property.usable_sqm  %}
<b>SQM</b> <a class="float-right">{{ property.usable_sqm }}</a>
{% endif %}
</li>

另一个应用程序完全相同。基本上我从那里复制粘贴了所有内容,然后只是更改了文件并将所有字段从 Property 重命名为 Tenant (我的意思是所有功能和 url ...)似乎是什么问题?因为按照我的逻辑,这应该可行。

【问题讨论】:

    标签: python django django-models django-views django-templates


    【解决方案1】:

    您提供的views.py 文档没有property_details.html 模板,而是有租户模板(您试图将租户对象渲染到属性模板中对吗?)。我不确定您如何尝试从提供的代码将租户模型传递给属性模板。

    为什么不将租户模型导入属性视图,然后将所需的任何租户对象传递给属性模板?

    【讨论】:

      猜你喜欢
      • 2013-02-17
      • 2019-09-03
      • 2020-03-03
      • 1970-01-01
      • 2016-05-06
      • 1970-01-01
      • 1970-01-01
      • 2016-10-15
      • 2018-12-12
      相关资源
      最近更新 更多