在管理面板中,当我输入正确的密码并单击登录时,它会将我重新加载到同一页面;当我输入的密码错误时,它显示一个错误;在我的项目中,当我登录时,它会将我带到主页但尚未登录,但在启动时曾经可以正常使用。
我的设置文件
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')
我的网址文件
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)
我的管理文件
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? 任何建议出什么事了**
答案 0 :(得分:0)
这可能是由于URL配置中缺少插入符号^
造成的。尖号只是标记行/字符串的开头。
将模式更改为以下内容,看看是否可以解决。
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