如何从根路径自动重定向?

时间:2019-03-07 02:15:58

标签: python django python-3.x path url-redirection

我正在使用Django和Python 3.7。我在尝试从主页(“ /”)重定向到其他路径时遇到问题。我希望默认的根路径重定向到“ / trending”。我尝试过:

urlpatterns = [
    path(r'/$', redirect_to, {'url': '/trending'}), 
    path('trending', views.trending, name='trending'),
]

但是,我遇到了错误:

name 'redirect_to' is not defined

1 个答案:

答案 0 :(得分:2)

将您的路径链接到视图函数定义并返回重定向。

urls.py

urlpatterns = [
    path('', redirect_view)
    # ... more URL patterns here
]

views.py

def redirect_view(request):
    response = redirect('/trending/')
    return response
相关问题