时间: 2020-11-25|9次围观|0 条评论

http://127.0.0.1:8000/boards/1/
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 %}

 

转载于:https://www.cnblogs.com/larken/p/9565148.html

原文链接:https://blog.csdn.net/weixin_30342827/article/details/96385242

本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时处理。

本博客所有文章如无特别注明均为原创。
复制或转载请以超链接形式注明转自起风了,原文地址《Django入门与实践-第21章:迁移(完结)
   

还没有人抢沙发呢~