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
时间: 2024-11-01 13:03:38