Django FormViews 显示下拉菜单而不是字符和文本字段

问题描述

我是 Django 的新手,我正在尝试使用 FormViews,但是当我显示视图时,它看起来很好,除了带有外键的字段(公司地址和公司联系信息)。它不是为每个变量创建一个 Char- 和 Textfields,而是创建一个下拉字段。

FormView

我查看了 formsets、inline_formsets 和 model_formsets,但似乎无法弄清楚。

 from django.db import models
 from django.forms import ModelForm


 class address(models.Model):
     streetName = models.CharField(max_length = 45)
     houseNumber = models.CharField(max_length = 25)
     postalCode = models.CharField(max_length = 4)
     region = models.CharField(max_length = 45)

     def __str__(self):
         return '{} {} {} {}'.format(self.streetName,self.houseNumber,self.postalCode,self.region)

 class contact@R_530_4045@ion(models.Model):
     phoneNumber = models.CharField(max_length = 11)
     email = models.CharField(max_length = 100)
     mainContact = models.CharField(max_length = 50)

     def __str__(self):
         return '{} {} {}'.format(self.phoneNumber,self.email,self.mainContact)

 class company(models.Model):
     companyName = models.CharField(max_length = 145)
     companyAddress = models.ForeignKey(address,on_delete = models.CASCADE)
     description = models.TextField()
     companyContact@R_530_4045@ion = models.ForeignKey(contact@R_530_4045@ion,on_delete = models.CASCADE)
     websiteURL = models.CharField(max_length = 100)
     relationToDjango = models.TextField()

     def __str__(self):
         return '{} {} {} {} {} {}'.format(self.companyName,self.companyAddress,self.description,self.companyContact@R_530_4045@ion,self.websiteURL,self.relationToDjango)

 class addressForm(ModelForm):
     class Meta:
         model = address
         fields = '__all__'

 class contact@R_530_4045@ionForm(ModelForm):
     class Meta:
         model = contact@R_530_4045@ion
         fields = '__all__'

 class companyForm(ModelForm):
     class Meta:
         model = company
         fields = ['companyName','companyAddress','description','companyContact@R_530_4045@ion','websiteURL','relationToDjango']

 class CompanyFormView(FormView):
     template_name = 'company/createCompanyProfile.html'
     form_class = companyForm
     success_url = '/'

     def form_valid(self,form):
         print("done!")
         return super().form_valid(form)

{% block content %}

<form method="post">{% csrf_token %}
    {{ form.as_p}}
    <input type="submit" value="Create Profile">
</form>

{% endblock %}

解决方法

问题可能出在 HTML 文件中。检查 CompanyAddress 和 CompanyFormView 是否有选择(下拉)标签。如果是 select 标签,则必须将其更改为文本类型的输入字段。

<label for="lname">Last name:</label>
<input type="text" id="lname" name="lname"><br><be>

请分享您的 HTML 文件,以便更准确地解决您的问题。