larken

勤奋的人生才有价值

导航

Django入门与实践-第21章:迁移(完结)

python manage.py migrate

#boards/models.py
class Topic(models.Model):
    views = models.PositiveIntegerField(default=0) # <- here
    

python manage.py makemigrations
python manage.py migrate


#boards/views.py 

def topic_posts(request, pk, topic_pk):
    topic = get_object_or_404(Topic, board__pk=pk, pk=topic_pk)
    topic.views += 1
    topic.save()
    return render(request, 'topic_posts.html', {'topic': topic})

<!--templates/topics.html-->
{% for topic in topics %}
    <tr>
        <td><a href="{% url 'topic_posts' board.pk topic.pk %}">{{ topic.subject }}</a></td>
        <td>{{ topic.starter.username }}</td>
        <td>{{ topic.replies }}</td>
        <td>{{ topic.views }}</td> <!-- here -->
        <td>{{ topic.last_updated }}</td>
    </tr>
{% endfor %}

 

posted on 2018-09-05 14:00  larken  阅读(213)  评论(0编辑  收藏  举报