models.py:
class Msg(models.Model):
title = models.CharField(max_length=30)
content = models.TextField()
user = models.ForeignKey(User)
ip = models.IPAddressField()
datetime = models.DateTimeField(auto_now_add=True)
clickcount = models.IntegerField(default=0)
def __unicode__(self):
return u'用户%s发表的标题为%s的留言' % (self.user.username, self.title)
class Meta:
verbose_name_plural = u'留言信息'
views.py:
class msg_detail_page(DetailView):
model = Msg
def m_d_p(request, pk):
msg = get_object_or_404(Msg, id=pk)
msg.clickcount +=1
msg.save()
urls.py:
url(r'^detail/(?P<pk>\d+)/$', msg_detail_page.as_view(
template_name='message/msg_detail_page.html'
)),
.html里:
{% extends "base.html" %}
{#{% load comments %}#}
{% block title %}留言内容与评论{% endblock %}
{% block head %}留言内容与评论{% endblock %}
{% block content %}
<table class="msglist">
<tr align="left">
<th>留言人:{{ msg.user.username }}</th>
<th>留言时间:{{ msg.datetime|date:"Y-m-d H:i" }}</th>
<th>IP地址:{{msg.ip }}</th>
<th>点击次数:{{msg.clickcount}}</th>
</tr>
<tr>
<td colspan="3"><b>{{ msg.title }}</b></td>
</tr>
<tr>
<td colspan="3">{{ msg.content }}</td>
</tr>
</table>
{% endblock %}
哪里有问题呢? 点击次数:{{msg.clickcount}} 总是为:0
我觉得是views.py写的不对。好像没返回还是怎么?但是不知道怎么写。请问怎么样才能让点击次数正常增加?
我是django1.5+python2.7
多谢