Django 错误:找不到页面 (404) 请求方法:GET 请求 URL:http://127.0.0.1:8000/helpdesk/login/?next=/

Posted

技术标签:

【中文标题】Django 错误:找不到页面 (404) 请求方法:GET 请求 URL:http://127.0.0.1:8000/helpdesk/login/?next=/【英文标题】:Django Error: Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/helpdesk/login/?next=/ 【发布时间】:2020-04-26 15:31:53 【问题描述】:

我是初学者。我正在尝试使用 Django 帮助台框架构建一个帮助台系统。但是当我点击登录按钮时,我收到以下错误:

找不到页面 (404) 请求方法:GET 请求网址:http://127.0.0.1:8000/helpdesk/login/?next=/ 使用 moog.urls 中定义的 URLconf,Django 按以下顺序尝试了这些 URL 模式:

admin/
^dashboard/$ [name='dashboard']
^tickets/$ [name='list']
^tickets/update/$ [name='mass_update']
^tickets/submit/$ [name='submit']
^tickets/(?P<ticket_id>[0-9]+)/$ [name='view']
^tickets/(?P<ticket_id>[0-9]+)/followup_edit/(?P<followup_id>[0-9]+)/$ [name='followup_edit']
^tickets/(?P<ticket_id>[0-9]+)/followup_delete/(?P<followup_id>[0-9]+)/$ [name='followup_delete']
^tickets/(?P<ticket_id>[0-9]+)/edit/$ [name='edit']
^tickets/(?P<ticket_id>[0-9]+)/update/$ [name='update']
^tickets/(?P<ticket_id>[0-9]+)/delete/$ [name='delete']
^tickets/(?P<ticket_id>[0-9]+)/hold/$ [name='hold']
^tickets/(?P<ticket_id>[0-9]+)/unhold/$ [name='unhold']
^tickets/(?P<ticket_id>[0-9]+)/cc/$ [name='ticket_cc']
^tickets/(?P<ticket_id>[0-9]+)/cc/add/$ [name='ticket_cc_add']
^tickets/(?P<ticket_id>[0-9]+)/cc/delete/(?P<cc_id>[0-9]+)/$ [name='ticket_cc_del']
^tickets/(?P<ticket_id>[0-9]+)/dependency/add/$ [name='ticket_dependency_add']
^tickets/(?P<ticket_id>[0-9]+)/dependency/delete/(?P<dependency_id>[0-9]+)/$ [name='ticket_dependency_del']
^tickets/(?P<ticket_id>[0-9]+)/attachment_delete/(?P<attachment_id>[0-9]+)/$ [name='attachment_del']
^raw/(?P<type>\w+)/$ [name='raw']
^rss/$ [name='rss_index']
^reports/$ [name='report_index']
^reports/(?P<report>\w+)/$ [name='run_report']
^save_query/$ [name='savequery']
^delete_query/(?P<id>[0-9]+)/$ [name='delete_query']
^settings/$ [name='user_settings']
^ignore/$ [name='email_ignore']
^ignore/add/$ [name='email_ignore_add']
^ignore/delete/(?P<id>[0-9]+)/$ [name='email_ignore_del']
^$ [name='home']
^view/$ [name='public_view']
^change_language/$ [name='public_change_language']
^rss/user/(?P<user_name>[^/]+)/$ [name='rss_user']
^rss/user/(?P<user_name>[^/]+)/(?P<queue_slug>[A-Za-z0-9_-]+)/$ [name='rss_user_queue']
^rss/queue/(?P<queue_slug>[A-Za-z0-9_-]+)/$ [name='rss_queue']
^rss/unassigned/$ [name='rss_unassigned']
^rss/recent_activity/$ [name='rss_activity']
^login/$ [name='login']
^logout/$ [name='logout']
^password_change/$ [name='password_change']
^password_change/done$ [name='password_change_done']
^kb/$ [name='kb_index']
^kb/(?P<item>[0-9]+)/$ [name='kb_item']
^kb/(?P<item>[0-9]+)/vote/$ [name='kb_vote']
^kb/(?P<slug>[A-Za-z0-9_-]+)/$ [name='kb_category']
^help/context/$ [name='help_context']
^system_settings/$ [name='system_settings']
The current path, helpdesk/login/, didn't match any of these.

下面是我的 settings.py

     import os

    # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


    # Quick-start development settings - unsuitable for production
    # See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/

    # SECURITY WARNING: keep the secret key used in production secret!
    SECRET_KEY = '7)%lt=ea(z6mi1k$3ho5jmlz^(5kz01h78&f6pl7u=k6$1hzao'

    # SECURITY WARNING: don't run with debug turned on in production!
    DEBUG = True

    ALLOWED_HOSTS = []


    # Application definition

    INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',
    'django.contrib.humanize',
    'markdown_deux',
    'bootstrapform', 
    'helpdesk',
    'moog',
    ]

    MIDDLEWARE = [
   'django.middleware.security.SecurityMiddleware',
   'django.contrib.sessions.middleware.SessionMiddleware',
   'django.middleware.common.CommonMiddleware',
   'django.middleware.csrf.CsrfViewMiddleware',
   'django.contrib.auth.middleware.AuthenticationMiddleware',
   'django.contrib.messages.middleware.MessageMiddleware',
   'django.middleware.clickjacking.XFrameOptionsMiddleware',
    ]

    ROOT_URLCONF = 'moog.urls'

    TEMPLATES = [
        
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [],
            'APP_DIRS': True,
            'OPTIONS': 
                'context_processors': [
                    'django.template.context_processors.debug',
                    'django.template.context_processors.request',
                    'django.contrib.auth.context_processors.auth',
                    'django.contrib.messages.context_processors.messages',
                ],
            ,
        ,
    ]

    WSGI_APPLICATION = 'moog.wsgi.application'


    # Database
    # https://docs.djangoproject.com/en/2.2/ref/settings/#databases

    DATABASES = 
        'default': 
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
        
    


    # Password validation
    # https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators

    AUTH_PASSWORD_VALIDATORS = [
        
            'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
        ,
        
            'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
        ,
        
            'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
        ,
        
            'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
        ,
    ]


    # Internationalization
    # https://docs.djangoproject.com/en/2.2/topics/i18n/

    LANGUAGE_CODE = 'en-us'

    TIME_ZONE = 'UTC'

    USE_I18N = True

    USE_L10N = True

    USE_TZ = True

    SITE_ID = 1

    LOGIN_REDIRECT_URL = '/helpdesk/login/'

    LOGIN_URL = '/helpdesk/login/'

    # Static files (CSS, javascript, Images)
    # https://docs.djangoproject.com/en/2.2/howto/static-files/

    STATIC_ROOT = 'Users/mahes/Dev/moog/django-helpdesk-master/helpdesk/static/helpdesk'

    STATIC_URL = '/static/' 

urls.py

"""moog URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/2.2/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from django.conf.urls import include,url

urlpatterns = [
    path(r'admin/', admin.site.urls),
    path(r'', include('helpdesk.urls')),
    #url(r'', include('mail.urls'))
]

从帮助台 sitepackage 添加 Urls.py:

"""
django-helpdesk - A Django powered ticket tracker for small enterprise.

(c) Copyright 2008 Jutda. All Rights Reserved. See LICENSE for details.

urls.py - Mapping of URL's to our various views. Note we always used NAMED
          views for simplicity in linking later on.
"""

from django.conf.urls import url
from django.contrib.auth.decorators import login_required
from django.contrib.auth import views as auth_views
from django.views.generic import TemplateView

from helpdesk import settings as helpdesk_settings
from helpdesk.views import feeds, staff, public, kb, login


class DirectTemplateView(TemplateView):
    extra_context = None

    def get_context_data(self, **kwargs):
        context = super(self.__class__, self).get_context_data(**kwargs)
        if self.extra_context is not None:
            for key, value in self.extra_context.items():
                if callable(value):
                    context[key] = value()
                else:
                    context[key] = value
        return context


app_name = 'helpdesk'

urlpatterns = [
    url(r'^dashboard/$',
        staff.dashboard,
        name='dashboard'),

    url(r'^tickets/$',
        staff.ticket_list,
        name='list'),

    url(r'^tickets/update/$',
        staff.mass_update,
        name='mass_update'),

    url(r'^tickets/submit/$',
        staff.create_ticket,
        name='submit'),

    url(r'^tickets/(?P<ticket_id>[0-9]+)/$',
        staff.view_ticket,
        name='view'),

    url(r'^tickets/(?P<ticket_id>[0-9]+)/followup_edit/(?P<followup_id>[0-9]+)/$',
        staff.followup_edit,
        name='followup_edit'),

    url(r'^tickets/(?P<ticket_id>[0-9]+)/followup_delete/(?P<followup_id>[0-9]+)/$',
        staff.followup_delete,
        name='followup_delete'),

    url(r'^tickets/(?P<ticket_id>[0-9]+)/edit/$',
        staff.edit_ticket,
        name='edit'),

    url(r'^tickets/(?P<ticket_id>[0-9]+)/update/$',
        staff.update_ticket,
        name='update'),

    url(r'^tickets/(?P<ticket_id>[0-9]+)/delete/$',
        staff.delete_ticket,
        name='delete'),

    url(r'^tickets/(?P<ticket_id>[0-9]+)/hold/$',
        staff.hold_ticket,
        name='hold'),

    url(r'^tickets/(?P<ticket_id>[0-9]+)/unhold/$',
        staff.unhold_ticket,
        name='unhold'),

    url(r'^tickets/(?P<ticket_id>[0-9]+)/cc/$',
        staff.ticket_cc,
        name='ticket_cc'),

    url(r'^tickets/(?P<ticket_id>[0-9]+)/cc/add/$',
        staff.ticket_cc_add,
        name='ticket_cc_add'),

    url(r'^tickets/(?P<ticket_id>[0-9]+)/cc/delete/(?P<cc_id>[0-9]+)/$',
        staff.ticket_cc_del,
        name='ticket_cc_del'),

    url(r'^tickets/(?P<ticket_id>[0-9]+)/dependency/add/$',
        staff.ticket_dependency_add,
        name='ticket_dependency_add'),

    url(r'^tickets/(?P<ticket_id>[0-9]+)/dependency/delete/(?P<dependency_id>[0-9]+)/$',
        staff.ticket_dependency_del,
        name='ticket_dependency_del'),

    url(r'^tickets/(?P<ticket_id>[0-9]+)/attachment_delete/(?P<attachment_id>[0-9]+)/$',
        staff.attachment_del,
        name='attachment_del'),

    url(r'^raw/(?P<type>\w+)/$',
        staff.raw_details,
        name='raw'),

    url(r'^rss/$',
        staff.rss_list,
        name='rss_index'),

    url(r'^reports/$',
        staff.report_index,
        name='report_index'),

    url(r'^reports/(?P<report>\w+)/$',
        staff.run_report,
        name='run_report'),

    url(r'^save_query/$',
        staff.save_query,
        name='savequery'),

    url(r'^delete_query/(?P<id>[0-9]+)/$',
        staff.delete_saved_query,
        name='delete_query'),

    url(r'^settings/$',
        staff.user_settings,
        name='user_settings'),

    url(r'^ignore/$',
        staff.email_ignore,
        name='email_ignore'),

    url(r'^ignore/add/$',
        staff.email_ignore_add,
        name='email_ignore_add'),

    url(r'^ignore/delete/(?P<id>[0-9]+)/$',
        staff.email_ignore_del,
        name='email_ignore_del'),
]

urlpatterns += [
    url(r'^$',
        public.homepage,
        name='home'),

    url(r'^view/$',
        public.view_ticket,
        name='public_view'),

    url(r'^change_language/$',
        public.change_language,
        name='public_change_language'),
]

urlpatterns += [
    url(r'^rss/user/(?P<user_name>[^/]+)/$',
        login_required(feeds.OpenTicketsByUser()),
        name='rss_user'),

    url(r'^rss/user/(?P<user_name>[^/]+)/(?P<queue_slug>[A-Za-z0-9_-]+)/$',
        login_required(feeds.OpenTicketsByUser()),
        name='rss_user_queue'),

    url(r'^rss/queue/(?P<queue_slug>[A-Za-z0-9_-]+)/$',
        login_required(feeds.OpenTicketsByQueue()),
        name='rss_queue'),

    url(r'^rss/unassigned/$',
        login_required(feeds.UnassignedTickets()),
        name='rss_unassigned'),

    url(r'^rss/recent_activity/$',
        login_required(feeds.RecentFollowUps()),
        name='rss_activity'),
]


urlpatterns += [
    url(r'^login/$',
        login.login,
        name='login'),

    url(r'^logout/$',
        auth_views.LogoutView.as_view(
            template_name='helpdesk/registration/login.html',
            next_page='../'),
        name='logout'),

    url(r'^password_change/$',
        auth_views.PasswordChangeView.as_view(
            template_name='helpdesk/registration/change_password.html',
            success_url='./done'),
        name='password_change'),

    url(r'^password_change/done$',
        auth_views.PasswordChangeDoneView.as_view(
            template_name='helpdesk/registration/change_password_done.html',),
        name='password_change_done'),
]

if helpdesk_settings.HELPDESK_KB_ENABLED:
    urlpatterns += [
        url(r'^kb/$',
            kb.index,
            name='kb_index'),

        url(r'^kb/(?P<item>[0-9]+)/$',
            kb.item,
            name='kb_item'),

        url(r'^kb/(?P<item>[0-9]+)/vote/$',
            kb.vote,
            name='kb_vote'),

        url(r'^kb/(?P<slug>[A-Za-z0-9_-]+)/$',
            kb.category,
            name='kb_category'),
    ]

urlpatterns += [
    url(r'^help/context/$',
        TemplateView.as_view(template_name='helpdesk/help_context.html'),
        name='help_context'),

    url(r'^system_settings/$',
        DirectTemplateView.as_view(template_name='helpdesk/system_settings.html'),
        name='system_settings'),
]

请帮助我如何调试和解决这个问题 提前致谢。

【问题讨论】:

问题可能与helpdesk.urls 文件中的模式有关。也请包括在内。 我没有收到询问。我已经发布了我的 settings.py 和 urls.py 文件。我找不到 helpdesk.urls 文件。请指定我在哪里可以找到它。 这是helpdesk 目录中的urls.py 文件。另请查看答案。 您已经为helpdesk.urls 编写了使用root 的模式,因此您无需在您的网址中添加/helpdesk。还将帮助台应用程序文件夹中的 urls.py 代码添加到您的问题中 【参考方案1】:

如日志所示

^login/$ [name='login']
^logout/$ [name='logout']

login URL 应该是 http://127.0.0.1:8000/login,但您访问的是 http://127.0.0.1:8000/helpdesk/login

编辑指向/login(不带/helpdesk)的前端路径或

# inside urls.py
path(r'helpdesk/', include('helpdesk.urls')),

【讨论】:

【参考方案2】:

如下更改您的设置

LOGIN_REDIRECT_URL = '/login/'
LOGIN_URL = '/login/'

【讨论】:

【参考方案3】:

与 bmons 的解决方案类似,我在项目 settings.py 的顶部(或底部)添加了以下代码(不带 LOGIN_URL = '/login/'):

# My settings LOGIN_URL = '/login/'

在 Django v3.0 项目的设置中编写 URL 时,我们不需要指定“用户”子目录。

【讨论】:

以上是关于Django 错误:找不到页面 (404) 请求方法:GET 请求 URL:http://127.0.0.1:8000/helpdesk/login/?next=/的主要内容,如果未能解决你的问题,请参考以下文章

找不到 Django 404 错误页面...当前路径与最后一个匹配

使用 Django,当我尝试访问应该存在的页面时,为啥找不到 404 错误页面?

URL 错误 404 找不到 Django 页面错误 x.html

找不到页面 (404) 请求方法:GET 请求 URL:http://127.0.0.1:8000/

Django React -- Heroku 上的部署错误(找不到页面 404)

Django:找不到页面(404)