简体   繁体   中英

django redirect url with parameter

when user click make thumbnail button ,it will call def thumb in the view and then render to http://127.0.0.1:8000/upload/thumb

I want to change to redirect to the url with the id number in database(Item.objects.all()) like : http://127.0.0.1:8000/upload/thumb/123

But not get it. Please help me.Thank you very much.

My code:

urls.py

urlpatterns = patterns('', 
    url(r'^$', views.background, name='background'),
    url(r'^thumb/(?P<result>\d+)$', views.thumb, name='thumb'),

views.py

def thumb(request,result):
    if request.method=="POST":
        photoid = request.POST['photoid']
        photowidth = request.POST['photowidth']
        item=Item.objects.filter(id=photoid) 
        return redirect(reverse('imageupload:thumb',kwargs={'result':photoid,'item':item }))

return HttpResponseRedirect(reverse('imageupload:background'))

templates:

  <form action="{% url 'imageupload:thumb'  i.id %}" method="POST" id="create_post">

When you use named parameter, you need to do like this:

return redirect(reverse('imageupload:thumb',kwargs={'result':item}))

And in your forms.py , you also need to modify your action to this:

action="{% url 'imageupload:thumb' result %}"

You can also access result in views.py like this:

def thumb(request, result):
    print result

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM