我正在第一个 Django 教程中制作投票应用程序。我看到每次投票时,页面都会刷新并转到页面顶部,我希望它只停留在原处,只更新段落标签。细节.html:
<html dir="rtl">
<h1>{{ article.title }}</h1>
<h2>{{ article.author }}</h2>
<h1>{{ article.text }}</h1>
<p>I have {{article.votes}}</p>
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<form action="{% url 'main:vote' article.id %}" method="post">
{% csrf_token %}
<input type="submit" value="Vote">
</form>
</html>
views.py 中的投票函数:
def vote(request, article_id):
article = get_object_or_404(Article, pk=article_id)
article.votes += 1
article.save()
# TODO: Change it so it doesnt return new refreshed html
return render(request, 'main/detail.html', {'article': article})
收到一只叮咚
相关分类