【问题标题】:redirect error with django使用 django 重定向错误
【发布时间】:2014-03-06 19:20:13
【问题描述】:

我对 Django 1.4.3 的重定向有问题

#-*- coding: utf-8 -*-
from django.http import HttpResponse, Http404
from django.shortcuts import redirect

def redirect_test(request):
    print("redirect_test() called.")

    return redirect('redirectionFunc')

def redirectionFunc(request):
    return HttpResponse("You have been redirected.")

我的网址是:

url(r'^redirectTest/$', 'redirect_test')

当我尝试打开时

http://xxx/blogz/redirectTest

我收到以下错误:

NoReverseMatch at /blogz/redirectTest/

Reverse for 'redirectionFunc' with arguments '()' and keyword arguments '{}' not found.

而我有终端:

redirect_test() called.

怎么了??

【问题讨论】:

标签: django redirect


【解决方案1】:

'redirect' 方法,获取 url 的 python 路径,或 url 的名称。

在第一种情况下,如果您的“redirect_func”视图在 app/views.py 中,您的代码应该是:

return redirect('app.views.redirect_func')

如果你想给你的 url 命名,例如“redirect_test”,在你的 url 配置中你应该给 url 方法加上 name 参数:

url(r'^redirectTest/$', 'app.views.redirect_func', name='redirect_test')

然后您可以使用您的网址名称调用重定向:

return redirect('redirect_test')

您可以查看有关命名网址here的文档

【讨论】:

  • 您提供的网址让我发现我没有为重定向本身设置任何网址...谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-30
  • 2014-02-27
  • 2020-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多