Django重定向到错误的URL

时间:2019-08-01 01:29:16

标签: python django python-3.x

我正在研究基本Django网站的登录/注销功能。 (CS50的匹诺曹披萨)。注销路径已注入URL中,我不确定原因。我的导航栏链接到主页,但是当我单击它时,它会将我重定向到注销页面。我单击的任何其他链接,该链接的路径都会添加到URL中,但会附加到注销路径中。 例如,单击我的站点的登录按钮(其路径为this.readAndPreview),URL变为:

login_default

尝试单击导航栏中应链接到http://127.0.0.1:8000/logoutlogin_default 页面的链接,我得到了

index

文件夹“ orders”是使“ pizza”知道的应用程序。所有html页面都在http://127.0.0.1:8000/logout 内部。

这是orders/templates/orders

orders/urls.py

from django.urls import path from . import views urlpatterns = [ path("", views.index, name = "index"), path("register_default", views.register_default, name = "register_default"), path("register_setup", views.register_setup, name = "register"), path("login_default", views.login_default, name = "login_default"), path("login_setup", views.login_setup, name = "login"), path("logout", views.logout_view, name="logout"), ] 中,“订单”应用的URL已被告知:

pizza/urls.py

这是from django.contrib import admin from django.urls import include, path urlpatterns = [ path("", include("orders.urls")), path("register_default", include("orders.urls")), path("register_setup", include("orders.urls")), path("login_default", include("orders.urls")), path("login_setup", include("orders.urls")), path("logout", include("orders.urls")), path("admin/", admin.site.urls), ]

orders/views.py

这是from django.contrib.auth import authenticate, login, logout from django.contrib.auth.models import User from django.http import HttpResponseRedirect from django.shortcuts import render from django.urls import reverse # Create your views here. def index(request): # If user is not logged in if not request.user.is_authenticated: return render(request, "orders/index.html", {"message": None}) context = { "user": request.user } return render(request, "orders/index.html", context) def register_default(request): return render(request, "orders/register.html") def register_setup(request): firstName = request.POST["userFirstName"] lastName = request.POST["userLastName"] email = request.POST["userEmail"] pw = request.POST["userPW"] user = User.objects.create_user(firstName, email, pw) user.save() return HttpResponseRedirect(reverse("index")) def login_default(request): return render(request, "orders/login.html") def login_setup(request): username = request.POST["loginName"] password = request.POST["loginPW"] user = authenticate(username=username, password=password) if user is not None: login(request, user) return HttpResponseRedirect(reverse("index")) else: return render(request, "orders/login.html", {"message": "Invalid Credentials"}) def logout_view(request): logout(request) return render(request, "orders/login.html", {"message": "Logged Out"}) ,所有其他模板均基于此。

base.html

这是<!DOCTYPE html> <html lang="en"> <head> {% block head %}{% endblock %} <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <!-- Bootstrap CSS --> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> <title>{% block title %}{% endblock %}</title> </head> <body> <nav class="navbar navbar-expand-md bg-light"> <a href="{% url 'index' %}" class="navbar-brand">Pinocchio's Pizza</a> <ul class="navbar-nav"> {% if message %} <li class="nav-item"> <a href="{% url 'logout' %}" class="nav-link">Log Out</a> </li> {% else %} <li class="nav-item"> <a href="{% url 'login_default' %}" class="nav-link">Log In</a> </li> <li class="nav-item"> <a href="{% url 'register_default' %}" class="nav-link">Sign Up</a> </li> {% endif %} </ul> </nav> {% block body %}{% endblock %} </body> </html>

login.html

这是{% extends "orders/base.html" %} {% block title %}Log In{% endblock %} {% block body %} {% if message %} <h1 class="text-danger">{{ message }}</h1> {% endif %} <div class="container-fluid"> <div class="row"> <div class="col-md-6 mx-auto mt-4"> <form action="{% url 'login' %}" method="POST"> {% csrf_token %} <div class="form-group"> <label for="loginName">First Name</label> <input type="text" name="loginName" id="loginName" class="form-control"> </div> <div class="form-group"> <label for="loginPW">Password</label> <input type="password" class="form-control" name="loginPW" id="loginPW"> </div> <button class="btn btn-success" type="submit">Log In</button> </form> </div> </div> </div> {% endblock %}

pizza/settings.py

1 个答案:

答案 0 :(得分:0)

尝试将pizza/urls.py更改为以下内容:

from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path("", include("orders.urls")),
    path("admin/", admin.site.urls),
]

原因是""路径应自动包含orders.urls中的所有URL。这是文档的链接:https://docs.djangoproject.com/en/2.2/topics/http/urls/#including-other-urlconfs

我注意到另一有助于捕获错误的事情是在网址末尾包含/,因此您可以在orders/urls.py中将其更改为以下内容。

from django.urls import path

from . import views

urlpatterns = [
    path("", views.index, name = "index"),
    path("register_default/", views.register_default, name = "register_default"),
    path("register_setup/", views.register_setup, name = "register"),
    path("login_default/", views.login_default, name = "login_default"),
    path("login_setup/", views.login_setup, name = "login"),
    path("logout/", views.logout_view, name="logout"),
]