User submissions are the sole responsibility of contributors, with TuteeHUB disclaiming liability for accuracy, copyrights, or consequences of use; content is for informational purposes only and not professional advice.
I have two models, "Fixture" and "PlayerVotes". The PlayerVotes model has two foreign keys, a link to a particular fixture and the voter (request.user).
I want to display the entire list of fixture as an expandable accordion (bootstrap4) and allow the logged in user to give some votes for each instance of fixture. I am not sure where I could have the form validation and the save() functions.
Form validation is done when you submit form using post request. The code you are showing is for rendering forms, you need a different view to handle the form submissions. You can do it like this:
# inside your votes viewfor game in fixtures:
games[game]= forms.PlayerVotesForm(user=request.user, round=game)return render(request,'votes/display.html', context={'games':games})
Template
{%for game, form in games.items %}<h1>{{ game.round }}h1>...<form action="{% url 'cast-vote' game.pk %}"method="post">{{ form.as_p }}<input type="submit" value="OK">form>{% enfor %}
Now you need to handle the form submission given above:
View
def cast_vote(request, pk):
game = get_object_or_404(Game, pk=pk)if request.method=="POST":
form =PlayerVotesForm(request.POST, user=request.user, round=game)if form.is_valid():# do other logicsreturn redirect('votes-url')
No matter what stage you're at in your education or career, TuteeHUB will help you reach the next level that
you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice
sessions to improve your knowledge and scores.
manpreet
Best Answer
3 years ago
I have two models, "Fixture" and "PlayerVotes". The PlayerVotes model has two foreign keys, a link to a particular fixture and the voter (request.user).
I want to display the entire list of fixture as an expandable accordion (bootstrap4) and allow the logged in user to give some votes for each instance of fixture. I am not sure where I could have the form validation and the save() functions.
models.py