首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >即使密码正确,也无法以用户或管理员身份登录Django项目

即使密码正确,也无法以用户或管理员身份登录Django项目
EN

Stack Overflow用户
提问于 2020-09-04 04:04:56
回答 1查看 53关注 0票数 0

在管理面板中,当我的密码正确时,我点击登录,它只是重新加载到同一页面,当我的密码错误时,它会显示一个错误,在我的项目中,当我登录时,它会将我带到主页,但没有登录,但在启动时它可以正常工作。

我的设置文件

代码语言:javascript
运行
复制
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 = '4uo!kh!^)!lc^xb0!&4aym-=%2(guhdfr^!2ly+rb0_!=@qnhx'
SECRET_KEY = os.environ.get('DJANGO_SECRET_KEY', '4uo!kh!^)!lc^xb0!&4aym-=%2(guhdfr^!2ly+rb0_!=@qnhx')
# SECURITY WARNING: don't run with debug turned on in production!
#DEBUG = True
DEBUG = os.environ.get('DJANGO_DEBUG', '') != 'False'
ALLOWED_HOSTS = ['.herokuapp.com','127.0.0.1']


# Application definition

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

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    '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 = 'locallibrary.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 = 'locallibrary.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 = 'Asia/Kolkata'

USE_I18N = True

USE_L10N = True

USE_TZ = True

LOGIN_URL = 'login'
LOGIN_REDIRECT_URL = ''
LOGOUT_URL = 'logout'
LOGOUT_REDIRECT_URL = 'login'
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

CSRF_COOKIE_SECURE = True
SESSION_COOKIE_SECURE = True

# Simplified static file serving.
# https://warehouse.python.org/project/whitenoise/
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

# Heroku: Update database configuration from $DATABASE_URL.
import dj_database_url
db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)

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

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

# The URL to use when referring to static files (where they will be served from)
STATIC_URL = '/static/'

STATICFILES_DIRS=[
    os.path.join(BASE_DIR, 'static')
]

MEDIA_URL = '/images/'

MEDIA_ROOT = os.path.join(BASE_DIR, 'static/images') 

我的URL文件

代码语言:javascript
运行
复制
from django.contrib import admin
from django.urls import path, include
from django.views.generic import RedirectView
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('admin/', admin.site.urls),
    path('catalog/', include('catalog.urls')),
    path('', RedirectView.as_view(url='catalog/', permanent=True)),
    # to direct directly to catalog because we don't have any other app
 ]
 # for authentication
urlpatterns += [
    path('accounts/', include('django.contrib.auth.urls')),
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
# to add static files
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

我的管理文件

代码语言:javascript
运行
复制
from django.contrib import admin
from .models import Author, Genre, Language, Book, BookInstance

# admin.site.register(BookInstance)
# admin.site.register(Book)
admin.site.register(Language)
# admin.site.register(Author)
admin.site.register(Genre)


class BooksInline(admin.TabularInline):
    model = Book


# Define admin class list displayed so that when we have many author we can easily identify
class AuthorAdmin(admin.ModelAdmin):
    list_display = ('last_name', 'first_name', 'date_of_birth', 'date_of_death')
    fields = ['first_name', 'last_name' ,'image', ('date_of_birth', 'date_of_death')] # show in same row
    inlines = [BooksInline]


admin.site.register(Author, AuthorAdmin)


class BooksInstanceInline(admin.TabularInline):
    model = BookInstance


# register the admin classes for Book Using the Decorator
@admin.register(Book)
class BookAdmin(admin.ModelAdmin):
    list_display = ('title', 'author', 'display_genre')
    # can't directly specify the genre filed in list_display because it is ManyToManyField
    inlines = [BooksInstanceInline]

# Register the admin classes for BookInstance using the decorator
@admin.register(BookInstance)
class BookInstanceAdmin(admin.ModelAdmin):
    list_display = ('book', 'status', 'borrower', 'due_back', 'id')
    list_filter = ('status', 'due_back') # list_filter will show option on side.
    fieldsets = (
        (None,{
            'fields': ('book', 'imprint', 'id')
        }),
        ('Availablity', {
            'fields': ('status', 'due_back', 'borrower')
        }),
    )
# Fieldsets None and Availability are titles, None because we don't want to give a title

**当我登录时,在本地服务器上显示状态为302和301?有什么建议吗?**

EN

回答 1

Stack Overflow用户

发布于 2020-09-04 11:12:56

这可能是由于URL配置中缺少插入符号^。插入符号只是标记行/字符串的开始。

将您的模式更改为以下内容,看看是否可以解决这个问题。

代码语言:javascript
运行
复制
urlpatterns = [
    path(r'^admin/', admin.site.urls),
    path(r'^catalog/', include('catalog.urls')),
    path(r'', RedirectView.as_view(url='catalog/', permanent=True)),
    # to direct directly to catalog because we don't have any other app
 ]

如果上面不起作用,也可以在这里看到答案-- Redirect to /admin/login/ results in 302

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63730924

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档