2012-07-06 42 views
0

我很喜歡使用模板來收集數據,但是在顯示時有一種巧妙的方式,Django將顯示字段並使用正確的值填充它們。我當然可以手動完成,但模型知道字段類型。我沒有看到這方面的任何文件。例如,我從模板收集數據:從檢索到的記錄填充Django模板

<strong>Company Name</strong> 
    <font color="red">{{ form.companyname.errors }}</font> 
    {{ form.companyname }} 

其中form是包含所有字段的公司模型。我將如何着手確保我可以使用這種類型的方法,使Django能夠呈現文本字段並填充當前值。例如,有沒有辦法在值發送通過以下方式:

myid = int(self.request.get('id')) 
    myrecord = Company.get_by_id(myid) 
    category_list = CompanyCategory.all() 
    path = os.path.join(os.path.dirname(__file__), 'editcompany.html') 
    self.response.out.write(template.render(path, {'form': myrecord, 'category_list': category_list})) 

我可以做同樣的用這個記錄,將模板與發送的值來填充?謝謝

+0

爲什麼不使用form.as_p – 2012-07-06 17:26:13

+0

或類似{%爲外地在形式上%} {{ }} {%endfor%} – 2012-07-06 17:31:59

+0

@JureC。我試過這個,但是表單看起來並沒有渲染出來。 – Androidian 2012-07-06 17:41:09

回答

3

這聽起來像你可能會不管你使用哪種形式的混淆約VS ModelForm

的差異和Form正確使用,表格模板側保持不變: 注意:所有的值在你的表單中(只要它綁定到POST或有一個實例)將在渲染時被預填充。

<form class="well" action="{% url member-profile %}" method="POST" enctype="multipart/form-data">{% csrf_token %} 
    <fieldset> 
     {{ form.non_field_errors }} 

     {{ form.display_name.label_tag }} 
     <span class="help-block">{{ form.display_name.help_text }}</span> 
     {{ form.display_name }} 
     <span class="error">{{ form.display_name.errors }}</span> 

     {{ form.biography.label_tag }} 
     <span class="help-block">{{ form.biography.help_text }}</span> 
     {{ form.biography }} 
     <span class="error">{{ form.biography.errors }}</span> 

     <input type="submit" class="button primary" value="Save" /> 
    </fieldset> 
</form> 
如果你想從一個記錄填充表格(或提交形式記錄)它可能是最好使用 ModelForm

EX不顯示用戶FK下拉列表中選擇配置文件形式

class ProfileForm(forms.ModelForm): 
    """Profile form"""  
    class Meta: 
     model = Profile 
     exclude = ('user',) 

的觀點:

def profile(request): 
    """Manage Account""" 
    if request.user.is_anonymous() : 
     # user isn't logged in 
     messages.info(request, _(u'You are not logged in!')) 
     return redirect('member-login') 

    # get the currently logged in user's profile 
    profile = request.user.profile 

    # check to see if this request is a post 
    if request.method == "POST": 
     # Bind the post to the form w/ profile as initial 
     form = ProfileForm(request.POST, instance=profile) 
     if form.is_valid() : 
      # if the form is valid 
      form.save() 
      messages.success(request, _(u'Success! You have updated your profile.')) 
     else : 
      # if the form is invalid 
      messages.error(request, _(u'Error! Correct all errors in the form below and resubmit.')) 
    else: 
     # set the initial form values to the current user's profile's values 
     form = ProfileForm(instance=profile) 

    return render(
     request, 
     'membership/manage/profile.html', 
     { 
      'form': form, 
     } 
    ) 

通知,外else初始化形式的實例:form = ProfileForm(instance=profile)和表單提交與初始化後的形式,但仍然結合實例form = ProfileForm(request.POST, instance=profile)

+0

謝謝,我現在擁有它。真的很簡單......有時候我會在Django上做一些更好的文檔,這些文章會與我產生共鳴。 – Androidian 2012-07-09 12:33:30