Django/python:我想循环表单中的字段并显示存储在数据库中的值

0 投票
2 回答
1805 浏览
提问于 2025-04-17 14:20

这是我的 models.py 文件:

class UserProfile(models.Model):
    user = models.ForeignKey(User, unique=True)
    utype = models.ForeignKey(Usertype)
    school = models.ForeignKey(School)
    courses = models.ManyToManyField(Course, related_name='c+', blank=True)
    tutors = models.ManyToManyField(Course, related_name='t+', blank=True)
    account = models.IntegerField(max_length=20, blank=True, null=True)
    cellphone = models.BigIntegerField(max_length=14, unique=True, blank=True, null=True)
    credits = models.FloatField(default=0.0)
    transactions = models.ManyToManyField(Transaction, related_name='t+', blank=True)
    verified = models.BooleanField(default=False)
    paypalVerified = models.BooleanField(default=False)
    applicationAccepted = models.BooleanField(default=False)
    address = models.CharField(max_length=256, blank=True)
    apartment = models.CharField(max_length=20, blank=True)
    city = models.CharField(max_length=256, blank=True)
    state = models.ForeignKey(State, null=True, blank=True)
    zip = models.IntegerField(max_length=10, blank=True, null=True)
    country = models.ForeignKey(Country, null=True, blank=True)

这是我的 forms.py 文件:

class ContactInfoForm(ModelForm):
def __init__(self, *args, **kwargs):
    super(ContactInfoForm, self).__init__(*args, **kwargs)
    self.fields['cellphone'].label = "Cell Phone Number"
    self.fields['address'].label = "Address"
    self.fields['apartment'].label = "Apartment"
    self.fields['city'].label = "City"
    self.fields['state'].label = "State"
    self.fields['zip'].label = "Zip Code"
    self.fields['country'].label = "Country"
class Meta:
    model = UserProfile
    fields = ('cellphone', 'address', 'apartment', 'city', 'state', 'zip', 'country')

这是我的 views.py 文件:

def profile(request):
c=getUserContext(request)
c['contactInfoForm'] = ContactInfoForm(instance = c['profile'])
if request.method == 'POST':
    if request.POST['formType'] == "ContactInfo":
        c['contactInfoForm'] = ContactInfoForm(request.POST, instance = c['profile'])
        if c['contactInfoForm'].is_valid():
            c['contactInfoForm'].save()
return render_to_response('profile.html', c)

如果用户在 ContactInfoForm 的所有字段都输入了数据,我该如何遍历表单中的字段并显示它们的值呢?

比如,我想显示:

手机号码 123-456-7890
地址 1 West Spring St ...等等。

我不能遍历模型中的字段,因为那里面有很多我不想在个人资料页面的联系信息部分显示的字段。

我为这个问题困扰了很久,但一直没有找到解决办法。如果我的问题不清楚,请告诉我,我会提供更多信息或尝试重新组织问题。

这是我目前在 profile.html 中向用户展示表单的方式:

{% for field in contactInfoForm %}
                    <tr>
                            <td class="fieldright">
                            <label class="inline">{{ field.label_tag }}</label>
                            </td>
                            <td>
                                {% if profile.field %}
                                    <div class="field">
                                        {{ profile.field }}
                                        <input type="hidden" name="{{ field }}" id="id_{{ field }}" value={{ profile.field }}>
                                    </div>
                                {% else %}
                                    {{ field }}
                                {% endif %}
                            </td>
                        </tr> 
                    {% endfor %}

编辑:我把 profile.field 改成了 field.value,现在值可以显示出来了。

2 个回答

0

遍历表单中的字段,而不是模型中的字段。

0

如果你想隐藏其他字段,只需在表单中使用排除(exclude)功能,而不是直接列出字段。

 class Meta:
     model = UserProfile
     exclude = ('', '')

撰写回答