user2492364
user2492364

Reputation: 6713

django redirect url with parameter

when user click make thumbnailbutton ,it will call def thumbin 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">

Upvotes: 1

Views: 5876

Answers (1)

shellbye
shellbye

Reputation: 4858

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

Upvotes: 4

Related Questions