简体   繁体   English

django使用参数重定向url

[英]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 当用户点击make thumbnail按钮时,它会在视图中调用def thumb ,然后渲染到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 我想更改为重定向到数据库中的id number (Item.objects.all())的URL,如: http://127.0.0.1:8000/upload/thumb/123http://127.0.0.1:8000/upload/thumb/123 :8000 /upload / tripd3

But not get it. 但是没有得到它。 Please help me.Thank you very much. 请帮帮我。非常感谢你。

My code: 我的代码:

urls.py urls.py

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

views.py 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: 而在你forms.py ,您还需要修改你的action ,以这样的:

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

You can also access result in views.py like this: 您还可以在views.py访问result ,如下所示:

def thumb(request, result):
    print result

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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