django form doesn't work on click submit

前端 未结 2 1204
醉酒成梦
醉酒成梦 2021-01-26 13:21

I\'m trying to do a form, with gender choices. The user could choice between male or female.

What I have now in forms.py:

class GenderForm(forms.Form):
          


        
相关标签:
2条回答
  • 2021-01-26 14:23

    You have to define url where post data will be sent

    This will send post data to url /my_app/my_view/

    <form action="/my_app/my_view/" method="post">
        {% for field in form_gender %}
            {{ field }}
        {% endfor %}
    
    <input type="submit" value="Submit" />
    </form> 
    

    This will send post data to current url you are on.

    <form action="." method="post">
        {% for field in form_gender %}
            {{ field }}
        {% endfor %}
    
    <input type="submit" value="Submit" />
    </form> 
    
    0 讨论(0)
  • 2021-01-26 14:27

    I don't understand why you've defined DemoDataForm in both models.py and forms.py, once as a ModelForm and once as a plain form. Because of that, it's impossible to tell from the code you've posted exactly which class you're instantiating.

    I would say, drop the version in forms.py, move the one in models.py into forms.py, and use that. But first you'll need to fix a slight bug - instead of:

    fields = ('gender')
    

    you need

    fields = ('gender',)
    

    because a one-item tuple always needs a comma, otherwise Python will try to iterate through the string.

    0 讨论(0)
提交回复
热议问题