我有一个名为“ Properties”的应用程序,并且已经创建了可以运行的DetailView。在我的“属性”模型中,我有一个“属性”模型和一个带有“属性的ForeignKey”的卧室模型。
#views.py
class PropertyDetailView(DetailView):
template_name = 'properties/property-detail.html'
model = Property
def get_context_data(self, **kwargs):
contacts = ContactsOwner.objects.filter(owner__property=self.object)
context = super().get_context_data(**kwargs)
context['contact'] = contacts
return context
我的模型。py:
class Property(models.Model):
property_reference = models.CharField(db_column='Property_Reference', max_length=10) # Field name made lowercase.
address = models.CharField(db_column='Address', max_length=250, blank=True, null=True) # Field name made lowercase.
post_code = models.CharField(db_column='Post_Code', max_length=15, blank=True, null=True) # Field name made lowercase.
type = models.CharField(db_column='Type', max_length=25, blank=True, null=True, choices=HOUSE_TYPE_CHOICES) # Field name made lowercase.
bedrooms = models.IntegerField(db_column='Bedrooms', blank=True, null=True) # Field name made lowercase.
bathrooms = models.IntegerField(db_column='Bathrooms', blank=True, null=True) # Field name made lowercase.
usual_cleaning_requirements = models.CharField(db_column='Usual_Cleaning_Requirements', max_length=250, blank=True, null=True) # Field name made lowercase.
notes = models.CharField(db_column='Notes', max_length=500, blank=True, null=True) # Field name made lowercase.
feature_image = models.ImageField(null=True)
class Meta:
db_table = 'Property'
def __str__(self):
return self.property_reference
def get_absolute_url(self):
return reverse("properties:property_detail",kwargs={'pk':self.pk})
class Bedroom(models.Model):
type = models.CharField(db_column='Type', choices=BEDROOM_TYPE_CHOICES, max_length=50)
bed_dimensions = models.CharField(db_column='Bed_Dimension', choices=BED_DIMENSION_CHOICES, max_length=30)
image = models.ImageField(null=True, blank=True)
ensuite = models.BooleanField(default=False)
notes = models.CharField(db_column='Notes', max_length=500, blank=True, null=True) # Field name made lowercase.
property = models.ForeignKey(Property, null=False, on_delete=models.CASCADE, related_name='bedroom')
我需要在模板“ property-detail.html”内创建一个名为“添加卧室”的按钮,该按钮会将我发送到带有预填充外键的表单。你能帮我吗?
答案 0 :(得分:0)
您需要将该按钮提交重定向到获取所需信息的视图,然后将其与接收到的信息(您的外键本身)相关的上下文重定向到另一个模板