我关注http://blog.narenarya.in/right-way-django-authentication.html以便在我的Django项目中添加用户身份验证,但是当我运行它并转到http://127.0.0.1:8000/log时,它会显示找不到的页面404!其他应用程序仍然正常工作 newsite / settings.py
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SECRET_KEY = 'k$1@+(lwis6$1e73$&f3xzk##qfs%$zv#=5n^st+05)zk%*@8@'
DEBUG = True
ALLOWED_HOSTS = []
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'aps',
'mail',
'log',
]
MIDDLEWARE_CLASSES = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'newsite.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['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 = 'newsite.wsgi.application'
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR + '/media/'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': '/home/hanou/Bureau/malouka/db.sqlite3',
'USER': 'root',
'PASSWORD': 'MOTDEPASSE',
'HOST': '127.0.0.1',
'PORT': '',
}
}
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 = 'fr-fr'
TIME_ZONE = 'Europe/Paris'
USE_I18N = True
USE_L10N = True
USE_TZ = True
STATIC_URL = '/static/'
STATICFILES_DIRS = (
BASE_DIR + '/static/',
)
EMAIL_HOST='smtp.gmail.com'
EMAIL_PORT=587
EMAIL_HOST_USER='*********@gmail.com'
EMAIL_HOST_PASSWORD='*********'
EMAIL_USE_TLS = True
LOGIN_REDIRECT_URL = '/'
newsite / urls.py
from django.conf.urls import include, url
from django.contrib import admin
from aps import views
from mail import views
from log import views
from django.contrib.auth import views
from log.forms import LoginForm
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^aps/', include('aps.urls')),
url(r'^mail/', include('mail.urls')),
url(r'^log/', include('log.urls')),
url(r'^login/$',views.login, {'template_name': 'login.html', 'authentication_form': LoginForm}),
url(r'^logout/$', views.logout, {'next_page': '/login'}),
]
newsite / log / views
from django.shortcuts import render
from django.contrib.auth.decorators import login_required
@login_required(login_url="login/")
def home(request):
return render(request,"home.html")
newsite / log / urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.home, name='home'),
]
答案 0 :(得分:1)
您的login_url缺少一个前导斜杠。这意味着您将从/log/
重定向到/log/login/
,这将提供404。
您可以通过将其更改为:
来解决此问题@login_required(login_url="/login/")
def home(request):
它适用于本教程,因为他们使用了
url(r'', include('log.urls')),
而不是
url(r'^log/', include('log.urls')),
但是,我不建议使用这样的相对网址。将绝对网址与斜杠一起使用会更好,甚至更好,使用reverse the url而不是硬编码。
要修复登录页面上的NoReverseMatch
错误,请将模板的表单标记更改为:
<form method="post" action="{% url 'login' %}">
并将名称添加到登录网址模式。
url(r'^login/$',views.login, {'template_name': 'login.html', 'authentication_form': LoginForm}, name='login'),
我还会在您的设置中添加LOGIN_URL
,
LOGIN_URL = 'login'
然后,每次使用login_required
时,您都不必指定它。
@login_required
def home(request):