creating django forms

后端 未结 3 1372
忘了有多久
忘了有多久 2020-12-23 15:03

I\'m struggling to get my head round django forms.. I\'ve been reading various documentation but just can\'t quite grasp the concepts. I have got to grips with models, views

3条回答
  •  南方客
    南方客 (楼主)
    2020-12-23 16:00

    Have you tried working with ModelForms before? As I understand, you're looking to create a form based on the model you created right?

    Lets say your model is called Temp. You can create a form that correlates with this model (and your question) like this:

    forms.py

    from django.forms import ModelForm
    
    class TempForm(ModelForm):
      class Meta:
        model = Temp
    

    The ModelForm will automatically map the selections/choices from your model to a form version.

    If you plan on using this in a template later, doing something like this will automatically create a drop-down menu with choices:

    {{ form.size }}

    Hope that answers your question!

提交回复
热议问题