无法以超级用户身份登录Django项目

时间:2019-12-25 15:02:01

标签: django

我正在学习Django以创建一个小型网站,但是我仍然对编码还很陌生。

我在集成的Django管理模块中创建超级用户时遇到问题。我遵循了SimpleIsBetterThanComplex OpenClassrooms的教程,并试图浏览Django官方文档,但没有成功:我无法以超级用户身份登录

创建超级用户后,无法访问管理员登录页面:http://localhost:8000/admin。我收到以下错误:

  

“网站无法访问。Localhost未授权连接。ERR_CONNECTION_REFUSED”

这会中断我的“ ​​python manage.py runserver”命令。

我在CMD中得到以下内容:

"GET /admin/ HTTP/1.1" 302 0
"GET /admin/login/?next=/admin/ HTTP/1.1" 200 1913
"GET /admin/ HTTP/1.1" 302 0
"GET /admin/login/?next=/admin/ HTTP/1.1" 200 1913
"POST /admin/login/?next=/admin/ HTTP/1.1" 302 0
(env) C:\Users\Me\fmg3\fmg3\

如果我从项目文件夹中删除数据库并应用迁移,则可以再次访问/ admin页面,所以我猜我在创建超级用户时发生了一些错误,但是我当然不能再使用我猜凭据是在数据库中创建的,超级用户。

下面是我的设置代码。py

"""
Django settings for fmg3 project.

Generated by 'django-admin startproject' using Django 3.0.

For more information on this file, see
https://docs.djangoproject.com/en/3.0/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.0/ref/settings/
"""

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/3.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = HIDDEN

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

ALLOWED_HOSTS = ['localhost','127.0.0.1']
# Added 'localhost' and '127.0.0.1' after help from fuser60596

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'fmgapp',
]

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 = 'fmg3.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 = 'fmg2.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.0/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/3.0/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/3.0/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


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

STATIC_URL = '/static/'

我的urls.py文件如下:

from django.contrib import admin
from django.conf.urls import url
from django.urls import path, include
from fmgapp import views

urlpatterns = [
    url(r'^admin/', admin.site.urls, name='admin'),
    url(r'^home/', views.home, name='home'),
]

我尝试创建一个仅具有一个非常简单的视图并将所有内容都设置为默认值的新项目,但是仍然无法正常工作。 我通过激活虚拟环境并使用以下命令来创建超级用户:

python manage.py createsuperuser

我使用python 3.7.0和Django 3.0.1

2 个答案:

答案 0 :(得分:0)

尝试在您的settings.py

中进行设置
ALLOWED_HOSTS = ['127.0.0.1']

http://127.0.0.1:8000/admin/应该起作用。

答案 1 :(得分:0)

String authentication = "username:password"; String authenticationEncoded = Base64.getEncoder().encodeToString(authentication .getBytes()); HttpHeaders headers = new HttpHeaders(); headers.add("Proxy-Authorization", "Basic " + authenticationEncoded ); 中每个主机的会话数据和cookie分别存储 ,这意味着如果您登录values=[] while True: A=input('Please type in a number.\n') if A == 'done': break try: B=int(A) values.append(B) except: print ('Invalid input') total=sum(values) average=total/(len(values)) print (total, len(values), average) ,则不会登录ALLOWED_HOSTS。您将需要为要使用的每个主机重新进行身份验证。

此外,我建议您在http://localhost:8000/admin中使用http://127.0.0.1/admin而不是path

url

url上的文档:

  

此函数是django.urls.re_path()的别名。将来的版本中可能不推荐使用它。