django页面跳转问题

1.页面跳转

项目的urls:

urlpatterns = [

    url(r'^app/', include(('app.urls'),namespace='my_app')),


]

app的urls:

urlpatterns = [
    #首页
    url(r'^index/',views.my_home,name='my_index'),
]

如果登录成功如何跳转到首页?

  • return HttpResponseRedirect(‘/app/index/’)

  • return HttpResponseRedirects(reverse(‘my_app:my_index’))

  • 注意:return Response(response,’home.html’),只是打开了一个新的页面,并非跳转.

2.遇到的问题:登录成功后,页面跳转到首页,但是url并没有改变.

错误原因:将return HttpResponseRedirect(‘/app/index/’)中地址写成了app/index/.前面的/不应该漏掉.

3.当想点击导航栏跳转到对应页面
<a href="{% url 'my_app:my_index' %}">首页</a>

猜你喜欢

转载自blog.csdn.net/u014229742/article/details/80976890
今日推荐