【问题标题】:HttpResponseRedirect url resetHttpResponseRedirect 网址重置
【发布时间】:2014-01-07 12:57:01
【问题描述】:

伙计们,这很简单,我在更新 CRUD 中的项目时使用 HttpResponseRedirect,

return HttpResponseRedirect('catalog/'+category.slugc+'/product/')

我得到的网址是:localhost:8000/catalog/product/1/catalog/snorkels/product/

我需要得到:localhost:8000/catalog/snorkels/product/

谁能帮帮我!

【问题讨论】:

    标签: python django python-2.7 django-forms django-views


    【解决方案1】:

    你忘了前面的/

    改变

    return HttpResponseRedirect('catalog/'+category.slugc+'/product/')
    

    return HttpResponseRedirect('/catalog/'+category.slugc+'/product/')
    

    另一种(更好的方法)是:

    return HttpResponseRedirect(reverse('url-name', args=(category.slugc, ))) #or reverse('myapp.views.view_name', args=(category.slugc, ))
    

    【讨论】:

    • 反向不起作用,因为您没有向 URL 提供所需的参数。
    【解决方案2】:

    首先,将您的 url 模式命名为 urls.py:

    url(r'^catalog/(?P<slug>\w+)/product/', 'some.view', name='slug-view')
    

    然后,在您看来,请执行以下操作:

    from django.shortcuts import redirect
    
    return redirect('slug-view', slug=category.slugc)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-29
      • 1970-01-01
      • 1970-01-01
      • 2012-05-21
      • 2018-07-31
      • 1970-01-01
      • 2011-11-24
      • 2016-09-27
      相关资源
      最近更新 更多