2013-01-31 21 views
0

這裏的值是我的models.py:Django的/ Python的:我要循環遍歷表單中的字段,並顯示存儲在數據庫中

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所有字段中輸入數據,我怎麼循環遍歷形式的字段nd顯示它們的值?

例如我想顯示

手機號碼123-456-7890
地址1西泉街 ...等

我不能過在我的模型領域的循環,因爲它包含了許多其它領域我不想顯示在個人資料頁面的contactinfo部分。

我一直在努力這一段時間,並沒有得到任何地方。如果我的問題不明確,請告訴我,我會提供更多信息或嘗試重新構建問題。

這裏是我當前如何顯示的形式提供給用戶的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和值是顯示了。

回答

0

迭代表單的字段而不是模型的字段。

+0

我以爲我遍歷窗體字段由{在contactInfoForm %%現場} –

+0

我是否做必須像{{field.value}}一樣來顯示值? –

0

如果你想隱藏其他領域只使用排除您的形式大於領域

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