当前路径admin没有匹配其中任何一个。 Django的

时间:2018-04-18 20:32:22

标签: python django

我使用django 2.0 我的urls.py文件:

"""django_direct URL Configuration

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

urlpatterns = [
    url('^admin/', admin.site.urls),
    # url(r'^/', include('main_app.urls')),
    url('/', views.index),
]

这是网址,我访问: https://django-direct-jonsdirewolf.c9users.io/admin 我使用cloud 9 Ide(它的在线ide),所以它就像localhost:8000 / admin enter image description here

Internal Server Error: /admin/
Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/django/core/handlers/exception.py", line 35, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.4/dist-packages/django/core/handlers/base.py", line 128, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/local/lib/python3.4/dist-packages/django/core/handlers/base.py", line 126, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/local/lib/python3.4/dist-packages/django/contrib/admin/sites.py", line 241, in wrapper
    return self.admin_view(view, cacheable)(*args, **kwargs)
  File "/usr/local/lib/python3.4/dist-packages/django/utils/decorators.py", line 142, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/usr/local/lib/python3.4/dist-packages/django/views/decorators/cache.py", line 44, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "/usr/local/lib/python3.4/dist-packages/django/contrib/admin/sites.py", line 212, in inner
    if not self.has_permission(request):
  File "/usr/local/lib/python3.4/dist-packages/django/contrib/admin/sites.py", line 186, in has_permission
    return request.user.is_active and request.user.is_staff
AttributeError: 'WSGIRequest' object has no attribute 'user'
[18/Apr/2018 20:38:23] "GET /admin/ HTTP/1.1" 500 86326
Not Found: /admin
[18/Apr/2018 20:38:34] "GET /admin HTTP/1.1" 404 2087
Internal

2 个答案:

答案 0 :(得分:0)

尝试将您的网址定义更改为:

url(r'^admin/', admin.site.urls),

答案 1 :(得分:0)

这有助于:

如果有任何人在使用Django 2.0时出现此问题,则使用新式MIDDLEWARE的以下配置似乎有效(文档在这里):

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',
]

感谢来自这里的人:https://stackoverflow.com/a/47795057/2950593