Data loading issues for Django form forms

Source: Internet
Author: User

today in the project encountered a problem, there is a Django additions and deletions to the template page, where the editing page uses a custom form to build the display content. The data of the form is queried from the database, and when the contents of the database are modified, the information displayed by the form is not modified. When the restart is completed, the data of the form is reloaded. a lot of data, no answer to the load time of form form data.

From the point of view, when the data of a form is loaded, it is loaded when the service starts. The code is as follows:

view.py

def testform (req): Form = Testforms.testform () print ' >>> ', form return Render_to_response ("test.html", {' Form ': Form})

urls.py

#form测试 URL (r ' ^test/form/$ ', ' manager.makoViews.testform '),

models.py

From Django Import forms Import Models Class Testform (forms. Form): Device_types = models. Role.objects.all (). Values_list (' id ', ' name ') name = forms. Charfield (Widget=forms.widgets.select (choices=device_types))

Html

<!  DOCTYPE html> 

Problem solving:

I met a buddy in the Django group today. Question How to write the select value in the form, I give an answer.

Name = forms. Charfield (Widget=forms.widgets.select (choices=device_types))

Then he said it could not update the value in time and continue to ask for advice. Get

Name2= forms. Modelchoicefield (label=u ' name2 ', queryset=models. Role.objects.all (), to_field_name= "id")

  instantly solved my big problem, thanks again to that Buddy.
By default, the option value of the select that is generated in the page is Queryset key, and the value is the value returned by the __unicode__ method in the Queryset model.

--- 2015-06-19 13:41   Records

---levels are limited and personal records are for reference only.

Data loading issues for Django form forms

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.