簡體   English   中英

Model 數據未顯示在模板上 - django

[英]Model data not displayed on template - django

短篇小說:我做了兩個應用程序。 django 項目中的屬性和租戶。 First I started rendering data from Property model to property_detail.html template and it works fine, but after I created & migrated the Tenants model, and I try to render data from there to property_detail.html it doesn't work. 然而它並沒有給我任何錯誤。 它只是沒有出現。

模型.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)})

網址.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
]

視圖.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...) 似乎是什么問題? 因為按照我的邏輯,這應該可行。

您提供的 views.py 文檔沒有 property_details.html 模板,而是有租戶模板(您試圖將租戶對象呈現為屬性模板對嗎?)。 我不確定您如何嘗試通過提供的代碼將租戶 model 傳遞給屬性模板。

為什么不將租戶 model 導入屬性視圖並將您想要的任何租戶對象傳遞給屬性模板?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM