Home > Mobile >  How to get the value of a radio button in django
How to get the value of a radio button in django

Time:04-10

I am learning django. I am stuck with this problem.

I want to create an upload form which has a field for gender. SO basically, it has radio buttons for male and female. Please note that I have not created forms.py file I just created a template in HTML and the rest of the processing happens in the views.py file. So, I want to know how can I pass the value of the radio button to the views.py file.

I have seen some tutorials and articles but they seem to be too complicated and also use forms.py file. Is there anything simpler.

I am a newbie and any help will be appreciated.

CodePudding user response:

You can put the radio button into the html directly, here is a mozilla doc that shows how to do it. However, just because you can do something doesn't mean you should. Why not?

Usually a form has user populated data that you store in a model so the recommendation is usually to have the model and the form which collects the data to be aligned somewhat. Keeping both components logically encoded server side is usually a better way to achieve that goal over time.

CodePudding user response:

you can put this in your model

    class MyModel(models.Model):


        gender = [("male", "male"),
                  ("female", "female"),
        ]

        month = models.CharField(max_length=6, choices=gender, default='male')

as in the Documentation

  • Related