短篇小说:我制作了两个应用程序。 django项目中的属性和租户。首先,我开始将数据从Property模型渲染到property_detail.html模板,并且可以正常工作,但是在创建并迁移了Tenants模型之后,我尝试将数据从那里渲染到property_detail.html,这是行不通的。但这并没有给我任何错误。它只是没有出现。
Models.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 ...),这似乎是什么问题?因为按照我的逻辑,这应该可行。
答案 0 :(得分:0)
您提供的views.py文档没有property_details.html模板,而是具有租户模板(您是否尝试将租户对象呈现到属性模板中?)。我不确定您如何尝试通过提供的代码将租户模型传递到属性模板。
为什么不将租户模型导入属性视图,并将想要的任何租户对象传递给属性模板?