Django:导航重新路由到错误的页面

时间:2018-08-18 12:55:34

标签: python django django-templates

我一直在关注此链接

https://www.youtube.com/watch?v=p_n7g6tVloU&list=PLw02n0FEB3E3VSHjyYMcFadtQORvl1Ssj&index=8

我的代码遇到问题该代码应该可以将我定向到login.html ,但它会将我导航回home.html

accounts / urls.py中的链接应该将我定向到login.html,但会将我导航回首页

我一直在工作的代码是

accounts / urls.py

urlpatterns= [
    url('',views.home),
    url(r'^login/$', login, {'template_name' : 'accounts/login.html'}),
]

根教程路径= tutorials / urls.py

urlpatterns = [
path(r'^admin/', admin.site.urls),
re_path(r'^account/',  include('accounts.urls'))
]

views.py

def home(request):
numbers = [1,2,3,4,5]
name = "Rajiv Pai"

args = {'myName' : name, 'numbers' : numbers}
return render(request,'accounts/home.html', args)

accounts / home.html

{% extends 'base.html' %}
<h1> Home Page</h1>
<!DOCTYPE html>
<html>
<head>
    {% block head %}
    <title> This is Home Page</title>
    {% endblock %}
</head>
<body>
    {% block body%}
    <h1> Hello World</h1>
    {% endblock %}
</body>
</html>

login.html

<!DOCTYPE html>
<html>
<head>
    {% load static %}
    <link rel="stylesheet" type="text/css" href="{% static 'accounts/style.css' %}">
<title>Login</title>
</head>
<body>
    <div class = "container">
    <h1>Welcome</h1> You can login here!!!
    <button class = "btn btn-danger outline" type = "button" name = "button">Click Here</button>
    <h1>Hello, {{myName}} </h1>
    <br/>
    <ul>
    {% for number in numbers %}
    <li>{{ number }}</li>
    {% endfor %}
    </ul>
</div>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

问题出在您的urls.py

urlpatterns= [ url('',views.home), url(r'^login/$', login, {'template_name' : 'accounts/login.html'}), ]

您的第一个网址格式错误,这就是为什么它重定向到首页而不是登录

urlpatterns= [
url('^$',views.home),
url(r'^login/$', login, {'template_name' : 'accounts/login.html'}),

]

要重定向到登录名,您可以使用上面的代码片段