2013-03-14 45 views
1

我想在處理帶反向的表單後傳遞一個變量,但在嘗試了很多事情之後沒有任何工作。這裏是我的代碼:在django的http響應重定向中使用反向

@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'), 

謝謝!

+0

弄來有何反應? – sneawo 2013-03-14 18:49:30

+1

希望:「如果request.method =='POSfrom django.core.urlresolvers導入reverseT'」是複製/粘貼錯誤? – Brandon 2013-03-14 18:55:36

+0

是的,我已經修改過我的帖子,對錯誤感到抱歉 – user2170928 2013-03-14 19:11:16

回答

2

你有track_id在URLconf:

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

嘗試:

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