Django:重定向仍保留在同一页面中

Django : redirect still remains in the same page

成功登录后,我将页面重定向到另一个名为 "feeds" 的应用程序。但是 url 相应地改变了,但内容仍然显示相同的登录页面。

views.py

def jlogin(request):
if request.method == 'POST':
    username = request.POST['uname']
    password = request.POST['pword']
    user = authenticate(username=username, password=password)
    if user is not None:
        login(request,user)
        print(user)
        return redirect('/feeds/')
    else:
        return render(request,'join/register.html')
else:
  return render(request,'join/login.html')

urls.py

urlpatterns = [
    url(r'^j

主要urls.py

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'',include('join.urls',namespace='join')),
    url(r'feeds',include('feeds.urls',namespace='feeds')),
    url(r'^discussion/', include('discussion.urls',namespace='discussion')),

    ]

如果我这样使用它会加载 "feeds"应用程序内容

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^login/',include('join.urls',namespace='join')),
    url(r'',include('feeds.urls',namespace='feeds')),
    url(r'^discussion/', include('discussion.urls',namespace='discussion')),

    ]

为什么会发生这些?

,join_views.register, name='register'), url(r'',join_views.jlogin, name='login'), ]

主要urls.py

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'',include('join.urls',namespace='join')),
    url(r'feeds',include('feeds.urls',namespace='feeds')),
    url(r'^discussion/', include('discussion.urls',namespace='discussion')),

    ]

如果我这样使用它会加载 "feeds"应用程序内容

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^login/',include('join.urls',namespace='join')),
    url(r'',include('feeds.urls',namespace='feeds')),
    url(r'^discussion/', include('discussion.urls',namespace='discussion')),

    ]

为什么会发生这些?

重定向不会呈现视图中的数据。您将需要使用模板和上下文呈现请求

return redirect('/feeds/')

return render(request, 'dir/template.html', {'context':value})

这是因为来自

的这两行

urls.py

url(r'',include('join.urls',namespace='join')),

join/urls.py

url(r'',join_views.jlogin, name='login'),

两个正则表达式捕获所有url。尝试任何其他 url,例如“/shmeeds/”,您将再次看到相同的页面:-)

要解决此问题,请将任何非空正则表达式添加到其中一个(或两个)url()