在django中的http响应重定向中使用reverse

时间:2013-03-14 18:41:51

标签: django django-forms django-views

我想在使用reverse处理表单后传递一个变量但是在尝试了很多东西之后没有任何作用。这是我的代码:

@login_required(login_url='/accounts/login/')
def pujar_track(request):
    if request.method=='POST':
        formulari = TrackForm(request.POST, request.FILES)
            if formulari.is_valid():                    
                track = formulari.save(commit=False)
                track.usuari = request.user
                track.save()

                url = reverse('acoplar_track', kwargs={'trac_id': track.id})
                return HttpResponseRedirect(url)

    else:
        formulari = TrackForm()

    return render(request,'principal/trackForm.html',
    {'formulari':formulari})


@login_required(login_url='/accounts/login/')
def acoplar_track(request, track_id):   
    track = get_object_or_404(Track, id=track_id)

    return render_to_response('principal/acoplarTrack.html',
        dict(track=track.nom),
        context_instance = RequestContext(request))

Url.py

url(r'^track/acoplar/(?P<track_id>\d+)/$',
    'principal.views.acoplar_track', name='acoplar_track'),

谢谢!

2 个答案:

答案 0 :(得分:2)

你的URLConf中有track_id

url(r'^track/acoplar/(?P<track_id>\d+)/$', 'principal.views.acoplar_track', name='acoplar_track'),

reverse语句中,您使用trac_id作为关键字参数。试试这个:

url = reverse('acoplar_track', kwargs={'track_id': track.id})

答案 1 :(得分:1)

尝试:

url = reverse('acoplar_track', args=(), kwargs={'track_id': track.id})