处理一个表单后,如何重定向到另一个视图获取NoReverseMatch错误 [英] How to redirect to a different view after processing a form getting NoReverseMatch error

查看:86
本文介绍了处理一个表单后,如何重定向到另一个视图获取NoReverseMatch错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个博客应用程序,在处理表单后,我想重定向到其他不同的URL,但是下面给出的视图不起作用。我无法使用HttpResponseRedirect,也不能直接重定向

I am making a blog app and I want to redirect to a different different url after I have processed the form, however the below given view is not working. I am unable to use neither HttpResponseRedirect nor simply redirect

@login_required
def blog_form(request,author_id=None,slug=None):

    context_instance=RequestContext(request)

    # This view will have a valid creator_id and slug field if the
    # blog is being edited and in this case the creator and user should be same
    if ( author_id and slug):
        author = User.objects.get(pk=author_id)
        blog = get_object_or_404(Entry, creator = author, slug = slug)
        if blog.creator != request.user:
            raise HttpResponseForbidden()

    # We set the user and created date and make a new object
    else:
        blog = Entry(creator=request.user,created_date=datetime.datetime.now() )

    if request.method == 'POST':

        #if the blog is not published
        if 'save' in request.POST:
            form = EntryForm(request.POST, instance = blog)
            if form.is_valid():
                form.save()

        elif 'publish' in request.POST:
            blog.pub_date = datetime.datetime.now()
            blog.status = 1
            form = EntryForm(request.POST, instance = blog)
            if form.is_valid():
                form.save()
                #return HttpResponseRedirect ('view_blog', (),{'author_id':blog.creator.id,'slug' :blog.slug,})
                return redirect ('blogs', blog.creator.id, blog.slug)
                #return render_to_response('blog/blog_view.html', {'blog': blog,},context_instance=RequestContext(request))

        elif 'preview' in request.POST: 
            form = EntryForm(request.POST, instance = blog)
            if form.is_valid():
                form.save()
                return render_to_response('blog/blog_view.html', {'blog': blog,},context_instance=RequestContext(request))

    else:
        form = EntryForm(instance = blog)

    return render_to_response('blog/blog.html', {'form':form}, context_instance)

但是,如果我只是使用render_to_response,那么它工作正常,但那不改变顶部的网址。我想要的是在发布帖子后将用户重定向到新页面,我也想改变顶部的网址。

However if I just use render_to_response then it works fine, but that does not change the url at the top. What I want is to redirect the user to a new page after publishing the post and I want to change the url at the top also.

推荐答案

使用HttpResponseRedirect是要走的路,但你需要结合使用它:

Using HttpResponseRedirect is the way to go, but you need to use that in conjunction with reverse:

from django.core.urlresolvers import reverse

### your other code
return HttpResponseRedirect(reverse('view_blog', args=(),
    kwargs={'author_id': blog.creator.id,'slug': blog.slug}))

希望能帮助你。

这篇关于处理一个表单后,如何重定向到另一个视图获取NoReverseMatch错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆