【问题标题】:Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/accounts/signup in DJANGO even though urls is mapped correctly找不到页面 (404) 请求方法:GET 请求 URL:DJANGO 中的 http://127.0.0.1:8000/accounts/signup,即使 url 映射正确
【发布时间】:2020-11-15 17:50:12
【问题描述】:

下面是子应用账号的urls.py文件

from django.urls import path
from . import views 

urlpatterns = [
    path("signup",views.signup,name='signup')
   
]

views.py帐户

def signup(request):
    if request.method == 'POST':

        firstname = request.POST['first_name']
        lastname = request.POST['last_name']
        username = request.POST['username']
        password1 = request.POST['password1']
        password2 = request.POST['password2']
        email = request.POST['email']

        if password1==password2:
                user = User.objects.create_user(firstname =first_name ,lastname =last_name ,username =username , email=email, password=password1)
                user.save();
                print("USER CREATED SUCCESSFULLY")
                return redirect('login.html')
    else:
        return redirect('signup.html')

我什至还映射了主 url.py 中的 url,但它仍然显示错误。 下面是signup.html

signup.html 文件

<form action="signup" method="POST">
            {% csrf_token %}
            <input type="text" name="first_name" placeholder="FIRST NAME"><br>
            <input type="text" name="last_name" placeholder="LAST NAME"><br>
            <input type="text" name="username" placeholder="USERNAME"><br>
            <input type="email" name="email" placeholder="EMAILID"><br>
            <input type="password" name="password1" placeholder="PASSWORD"><br>
            <input type="password" name="password2" placeholder="CONFIRM PASSWORD"><br>
            <input type="submit">

谁能帮我解决这个问题,我不明白出了什么问题。 显示页面未找到错误。

【问题讨论】:

  • 终端是否运行正常,没有任何错误?您是否执行了 makemigrations 和 migrate 步骤?
  • @AvinMathew 是的!终端工作得非常好,没有任何错误,我什至尝试过 makemigrations 和 migrate,但它不工作。即使正确映射了 url,它也显示找不到页面的问题。我也在setting.py中包含了这个名字。

标签: python html django server pgadmin


【解决方案1】:

您需要在项目 urls.py 中包含 accounts.urls(即)哪个文件夹包含 settings.py

例如: 让我假设您创建项目 Todo

Todo    
 - accounts
    - __init__.py
    - models.py
    - urls.py
    - views.py
    - apps.py
    - tests.py

 - Todo
    - __init__.py
    - urls.py
    - wsgi.py
    - settings.py

待办事项/urls.py 你需要包含accounts.urls

from django.conf.urls import include, path
app_name = "Todo"

urlpatterns = [
    # Examples:
     path(r'^accounts/', include('accounts.urls', namespace="Account")),
]

您需要在文件settings.py应用程序定义列表中记下应用程序

# Application definition

INSTALLED_APPS = [
    'accounts',
     ......
    'django.contrib.auth',
]

【讨论】:

  • 我已经包括了所有这些,这些东西都被正确提及了。但不知道出了什么问题。
猜你喜欢
  • 2021-03-05
  • 2016-10-30
  • 2019-08-01
  • 2020-08-15
  • 2023-03-22
  • 2018-07-13
  • 2021-07-25
  • 2021-10-30
相关资源
最近更新 更多