问题
I am new to Django and struggling with a basic problem, yet cannot find a solution online.
I have these models:
class Suggestion(models.Model):
author = models.ForeignKey('auth.User')
title = models.CharField(max_length=200)
description = models.TextField()
created_date = models.DateTimeField(default=timezone.now)
def __str__(self):
return self.title
class Vote(models.Model):
suggestion = models.ForeignKey(Suggestion)
voter = models.ForeignKey('auth.User')
vote_count = models.IntegerField(default=1)
and I'm trying create a view that would add a Vote to a given Suggestion, capturing the User who voted. I've seen some seem to do this with a form or with a regular function, so not sure what's the best practice here?
EDIT
Here is my attempt (not working). Any help/advice appreciated.
#forms.py
class VoteForm(forms.ModelForm):
class Meta:
model = Vote
fields = ()
#models.py
class Vote(models.Model):
suggestion = models.ForeignKey(Suggestion)
voter = models.ForeignKey('auth.User')
vote_count = models.BooleanField()
#views.py
def add_vote(request, pk):
if request.method == 'POST':
form = VoteForm(request.POST)
suggestion = get_object_or_404(Suggestion, pk=pk)
if form.is_valid():
vote = form.save(commit=False)
vote.voter = request.user
vote.vote_count = True
vote.save()
return render(request, 'suggestion/suggestion_detail.html', {'suggestion': suggestion})
#vote_form.html
<form method="post">
{% csrf_token %}
{{ form }}
<button type="submit">Vote</button>
</form>
回答1:
You should create a form for the vote and include it on the Suggestion view. The form can have it's own html -- vote_form.html. Then include it on the suggestion html page with
{% include '[name of directory]/vote_form.html' %}
As for the vote count, it shouldn't be an integer field unless you want users to cast multiple votes. If you just want someone to be able to vote once per suggestion, you should make the vote_count a boolean field (either true or false). Then you can assign true to a vote and false to a non-vote.
回答2:
I've managed to do what I wanted in this way:
#vote_form.html
<form action="{% url 'add_vote' suggestion.id %}" method="post">
{% csrf_token %}
<input type="submit" value="I want to vote">
</form>
#urls.py
urlpatterns = [
url(r'^suggestion/(?P<pk>\d+)/$', views.SuggestionDetail.as_view(), name="suggestion_detail"),
url(r'^suggestion/(?P<pk>\d+)/vote/$', views.add_vote, name='add_vote'),
]
#models.py
class Vote(models.Model):
suggestion = models.ForeignKey(Suggestion)
voter = models.ForeignKey('auth.User')
vote_count = models.BooleanField()
#views.py
def add_vote(request, pk):
suggestion = get_object_or_404(Suggestion, pk=pk)
vote = Vote(
suggestion = suggestion,
voter = request.user,
vote_count = True)
has_user_voted = Vote.objects.filter(voter=request.user, suggestion=suggestion).count()
if has_user_voted < 1:
vote.save()
else:
messages.error(request, 'It seems you have already voted, only one vote is allowed')
return HttpResponseRedirect(reverse('suggestion_detail', args=(suggestion.id,)))
来源:https://stackoverflow.com/questions/46196706/basic-logics-with-django-views-and-foreignkey