0
def upload(request):
        user = request.user
        upload_form = UploadForm(request.POST or None, request.FILES or None)
        if request.method == "POST":
                if upload_form.is_valid():
                        my_model = upload_form.save(commit=False)
                        my_model.user = user
                        print my_model.src
                        my_model.save()
                        return render_to_response('index.html',, context_instance=RequestContext(request))

        return render_to_response('index.html', {'upload_form': upload_form}, context_instance=RequestContext(request))

上传后如何重定向到/summary/1/(/summary/saved object id/)

4

2 回答 2

2

一种方法是

return HttpResponseRedirect('/summary/'+id)

更好的变体是

return HttpResonseRedirect('/summary/%i/' % id)

最好的变体是(这样您就不必在视图中硬编码您的网址)

return HttpResponseRedirect(reverse(yourViewNameHere, args=[id]))

如果你想使用

reversed_url = reverse(yourViewNameHere, kwargs = {'id':id})
return HttpResponseRedirect(reversed_url)
于 2013-06-27T06:30:19.083 回答
1

进口

from django.http import HttpResponseRedirect

然后

return HttpResponseRedirect('/summary/%s/' %object_id)
于 2013-06-27T06:31:21.600 回答