Django在页面刷新时发布重复

时间:2015-02-17 15:54:48

标签: django django-views

我有这个视图,它添加了对主题的回复:

@login_required
def post_reply(request, topic_id):
    tform = PostForm()
    topic = Topic.objects.get(pk=topic_id)
    args = {}
    if request.method == 'POST':
        post = PostForm(request.POST)

        if post.is_valid():

            p = post.save(commit = False)
            p.topic = topic
            p.title = post.cleaned_data['title']
            p.body = post.cleaned_data['body']
            p.creator = request.user
            p.user_ip = request.META['REMOTE_ADDR']

            p.save()

            tid = int(topic_id)


        args['topic_id'] = tid
        args['topic'] = topic
        args['posts'] = Post.objects.filter(topic_id= topic_id).order_by('created')
        return render_to_response("myforum/topic.html",args)            

    else:
        args.update(csrf(request))
        args['form'] = tform
        args['topic'] = topic
        return render_to_response('myforum/reply.html', args, 
                                  context_instance=RequestContext(request))

问题在于,当用户在发布回复后引用页面时,她的回复正在重复。怎么避免这个?

更新:以下是相关模板:

{% extends "base.html"%}
{% load static %}


{% block content%}

<div class="panel">
    <div class="container">
<!-- Posts  -->
<div class="col-md-12">
    <h3>{{ topic.title }}</h3>
<table class="table table-striped">

    <tr class="col-md-9"><td>{{ topic.description }}</td></tr>
    <tr class="col-md-3"><div class="userpanel"><td>{{ topic.created }}</td></div></tr>

    {% for post in posts %}
         <tr class="col-md-12 userpanel"><td>{{ post.title }}</td></tr>


        <tr class="">
            <td>
               <div class="col-md-9 userpanel">{{ post.body }} <br> {{ post.created }} </div> 
            </td>
            <td>
                <div class="col-md-3 userpanel">{{ post.creator }} <br> {{ post.created }} </div>
            </td>
        </tr>

    {% endfor %}
</table>

<hr />
<!-- Next/Prev page links  -->
{% if posts.object_list and posts.paginator.num_pages > 1 %}
<div class="pagination">
    <span class="step-links">
        {% if posts.has_previous %}
            <a href= "?page={{ posts.previous_page_number }}">previous &lt;&lt; </a>
        {% endif %}

        <span class="current">
            &nbsp;Page {{ posts.number }} of {{ topics.paginator.num_pages }}
        </span>

        {% if posts.has_next %}
            <a href="?page={{ posts.next_page_number }}"> &gt;&gt; next</a>
        {% endif %}
    </span>
</div>
{% endif %}
<a class="button" href="/forum/reply/{{topic.id}}"> Reply </a>

</div> <!-- End col-md-12  -->

</div> <!-- End container  -->
</div>

{% endblock %}

1 个答案:

答案 0 :(得分:5)

始终 - 我重复,总是在POST后重定向。

因此,当您的表单有效时,不要执行return render_to_response("myforum/topic.html",args),而是执行return HttpResponseRedirect(url_of_your_view)https://docs.djangoproject.com/en/1.7/ref/request-response/#django.http.HttpResponseRedirect

在OP的评论后更新:

您应该使用反向(https://docs.djangoproject.com/en/1.7/ref/urlresolvers/#reverse)创建要重定向到的网址:

return HttpResponseRedirect(reverse('myforum.views.topic', args=[topic_id]))

另外,我建议命名您的视图(并删除字符串以便定义它们,因为它们已被弃用),因此请更改您的urls.py行,如下所示: from myforum.views import topic ... url(r'^topic/(\d+)/$', topic, name='view_topic'), ...

然后只需reverse('view_topic', args=[topic_id])来获取您的观点的网址。