1、定义数据模型
首先定义投票人 属于UserProfile(它属于User)
再次定义投票的文章它属于Article
article = models.ForeignKey(to=Article, related_name='tickets')
定义投票的类型
VOTE_CHOICES = (
('like', 'like'),
('dislike', 'dislike'),
('normal', 'normal'),
)
choice = models.CharField(choices=VOTE_CHOICES, max_length=10)
总体代码如下:
class UserProfile(models.Model):
belong_to = models.OneToOneField(to=User, related_name="profile1")
profile_image = models.ImageField()
class Ticket(models.Model):
voter = models.ForeignKey(to=UserProfile, related_name="voted_tickets")
article = models.ForeignKey(to=Article, related_name='tickets')
VOTE_CHOICES = (
('like', 'like'),
('dislike', 'dislike'),
('normal', 'normal'),
)
choice = models.CharField(choices=VOTE_CHOICES, max_length=10)
2、页面结果的显示逻辑
在views.py中引入Ticket
from firstapp.models import Article, Comment, Ticket
这就就可以使用Ticket的数据并存储了
在详情页面detail中
def detail(request, id):
context = {}
if request.method == "GET":
form = CommentForm
article = Article.objects.get(id=id)
try:
voter_id = request.user.profile1.id#得到投票用户的ID
user_ticket_for_this_article = Ticket.objects.get(voter_id=voter_id,article_id=id)# 投票到文章的ID和投票人ID
context["user_ticket"] = user_ticket_for_this_article
except:
pass
context["article"] = article
context['form'] = form
return render(request, 'detail.html', context)
此时user_ticket
将可以使用models.py
中Ticket
的内容
在模板中
<form class="ui form" action="#" method="post">
{% if user_ticket.choice == 'like' %}
<button class="ui red tiny button" type="submit" name="vote" value="normal" >
<i class="icon checkmark"></i>
Get it!
</button>
<button class="ui tiny button" type="submit" name="vote" value="dislike" >
<i class="icon bomb"></i>
Hmmm...
</button>
{% elif user_ticket.choice == 'dislike' %}
<button class="ui tiny button" type="submit" name="vote" value="like" >
<i class="icon checkmark"></i>
Get it!
</button>
<button class="ui red tiny button" type="submit" name="vote" value="normal" >
<i class="icon bomb"></i>
Hmmm...
</button>
{% else %}
<button class="ui tiny button" type="submit" name="vote" value="like" >
<i class="icon checkmark"></i>
Get it!
</button>
<button class="ui tiny button" type="submit" name="vote" value="dislike" >
<i class="icon bomb"></i>
Hmmm...
</button>
{% endif %}
<button class="ui secondary circular tiny right floated pin icon button">
<i class="pin icon"></i>
Saved
</button>
</form>
在后台中的
设定一个choice为like
刷新前台
3、投票的存储逻辑
在views.py中建立投票
引入
from django.core.exceptions import ObjectDoesNotExist
建立处理逻辑
def detail_vote(request, id):
voter_id = request.user.profile1.id
try:
user_ticket_for_this_article = Ticket.objects.get(voter_id=voter_id,article_id=id)# 投票到文章的ID和投票人ID
user_ticket_for_this_article.choice = request.POST['vote']
user_ticket_for_this_article.save()
except ObjectDoesNotExist:
new_ticket = Ticket(voter_id=voter_id, article_id=id,choice=request.POST)
new_ticket.save()
return redirect(to='detail', id=id)
在urls.py中
引入detail_vote,写入url
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^index/', index, name="index"),
url(r'^detail/(?P<id>\d+)/$', detail, name="detail"),
url(r'^detail/vote/(?P<id>\d+)/$', detail_vote, name="vote"),
url(r'^comment/(?P<id>\d+)/$', comment, name="comment"),
url(r'^login/$', index_login, name="login"),
url(r'^register/$', index_register, name="register"),
url(r'^logout/$', logout,{'next_page': '/login'}, name="logout"),
在模板层中写入action处理
<form class="ui form" action="{% url 'vote' article.id %}" method="post">
4、统计功能
在views.py中添加
like_counts = Ticket.objects.filter(choice='like', article_id=id).count()
context['like_counts'] = like_counts
在模板中引用
<span style="color:#bbbbbb">{{ like_counts }} people got it</span>