Python Daily
2.57K subscribers
1.48K photos
53 videos
2 files
38.9K links
Daily Python News
Question, Tips and Tricks, Best Practices on Python Programming Language
Find more reddit channels over at @r_channels
Download Telegram
How to pass data from a button in the html page to a database?

I have to code a page that has a question, and 3 options to answer.
I have a page that display the questions and the 3 answers of every question, the questions are already stored in the database. Now my problem is how can I save the user choice for that specific question?
I have created a model to save the user choice, the user id, and the question id. But I'm having trouble to get witch button the user clicked for the answer.

#Here is the model
class UserVotes(models.Model):
id_user = models.CharField(max_length=10)
id_question = models.CharField(max_length=10)
choice_user = models.CharField(max_length=10)

#Here is the view
class QuestionListView(generic.ListView):
model = Question

def voteQuestion(request):
if 'Yes' in request.POST:
idQuestion = request.question.id
idUser = request.user.id
p = UserVotes(id_user=idUser, id_question=idQuestion, choice_user=1)
p.save()
elif 'No' in request.POST:
#same thing, the difference is the value of choice_user will be 2.

#the url.py
url(r'^question/$', views.QuestionListView.as_view(), name='question'),

#the html page
<button type="submit" name="Yes">Yes</button>
<button type="submit" name="No">No</button>

I've got several errors, the last one that came up is a http 405 error, method post not allowed.
If someone can help please, I've spend 2 nights and 1 day on this already haha. Thanks!!

/r/django
https://redd.it/8gae5m