1. 程式人生 > >Django入門與實踐-第21章:遷移(完結)

Django入門與實踐-第21章:遷移(完結)

ast spa rfi django入門 () pic posit object HERE

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 %}

Django入門與實踐-第21章:遷移(完結)