我刚刚设置了自己的Django服务器,但管理页面遇到了麻烦。当我尝试添加或删除用户时,或者基本上每当我从Django管理页面发送POST请求时,都会出现以下错误:
Raised by: django.contrib.admin.options.changelist_view
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^admin/
^register/$ [name='register']
^$
^login/$ [name='login']
^static\/(?P<path>.*)$
The current path, auth/user/, didn't match any of these.
正常页面工作正常。我仅在“管理”页面上收到错误。我尝试做python manage.py migrate
,但没有要应用的迁移。我猜想这与urls.py
或settings.py
文件有关。 re_path('^admin/', admin.site.urls)
网址格式已经存在,我不确定为什么它仍然显示错误。
路径结构:
-root
-mysite
-urls.py
-settings.py
-wsgi.py
-views.py
-forms.py
-templates
-static
-passenger_wsgi.py
-manage.py
这是我的urls.py文件:
from django.urls import re_path
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
admin.autodiscover()
urlpatterns = [
re_path('^admin/', admin.site.urls),
re_path('^register/$', register_page, name='register'),
re_path('^$', home_page),
re_path('^login/$', login_page, name='login'),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
这是settings.py
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SECRET_KEY = 'key'
DEBUG = True
ALLOWED_HOSTS = ['myip']
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'mysite',
]
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 = 'mysite.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'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 = 'mysite.wsgi.application'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
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',
},
]
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
STATIC_URL = '/static/'
'''
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
'''
STATIC_ROOT = '/home/referpay/rp/static'
答案 0 :(得分:0)
我在您的应用程序中找不到models.py。我注意到了一件奇怪的事,您正在将应用程序的视图和表单代码放在settings文件夹中。在django中,我们使用 django-admin.py startproject mysite 启动了一个项目后,我们使用命令 django-admin.py startapp app_name 添加了一个新应用。 Django将创建一个具有应用程序名称的文件夹,您将在其中编写该应用程序的模型,表单和视图。
我建议您遵循django教程:https://docs.djangoproject.com/en/2.1/intro/tutorial01/
答案 1 :(得分:0)
在这里黑暗中射击,但是如果尝试这样做会发生什么?
from django.urls import path
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('register/', register_page, name='register'),
path('', home_page),
path('login/', login_page, name='login'),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
答案 2 :(得分:0)
这是因为SCRIPT_NAME被设置为相对路径而不是应用程序的根目录。我必须对passenger_wsgi.py进行以下更改:
import imp
import os
import sys
import mysite.wsgi
sys.path.insert(0, os.path.dirname(__file__))
SCRIPT_NAME = ''
class PassengerPathInfoFix(object):
"""
Sets PATH_INFO from REQUEST_URI since Passenger doesn't provide it.
"""
def __init__(self, app):
self.app = app
def __call__(self, environ, start_response):
from urllib.parse import unquote
environ['SCRIPT_NAME'] = SCRIPT_NAME
request_uri = unquote(environ['REQUEST_URI'])
script_name = unquote(environ.get('SCRIPT_NAME', ''))
offset = request_uri.startswith(script_name) and len(environ['SCRIPT_NAME']) or 0
environ['PATH_INFO'] = request_uri[offset:].split('?', 1)[0]
return self.app(environ, start_response)
application = mysite.wsgi.application
application = PassengerPathInfoFix(application)