简体   繁体   English

找不到页面404 Django?

[英]Page not found 404 Django?

I followed http://blog.narenarya.in/right-way-django-authentication.html in order to add a user authentication in my Django project but when I run it and go to http://127.0.0.1:8000/log it shows page not found404! 我遵循了http://blog.narenarya.in/right-way-django-authentication.html ,以便在Django项目中添加用户身份验证,但是当我运行它并转到http://127.0.0.1:8000/日志显示未找到页面404! the others applications still working normally newsite/settings.py 其他应用程序仍然可以正常运行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 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 newsite / log / urls.py

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.home, name='home'),
]

Your login_url is missing a leading slash. 您的login_url缺少斜杠。 That means that you will redirected from /log/ to /log/login/ , which will give a 404. 这意味着您将从/log/重定向到/log/login/ ,这将得到404。

You can fix it by changing it to: 您可以将其更改为:

@login_required(login_url="/login/")
def home(request):

It works in the tutorial because they used 它在本教程中有效,因为他们使用了

url(r'', include('log.urls')),

instead of 代替

url(r'^log/', include('log.urls')),

However, I wouldn't recommend using relative urls like this. 但是,我不建议使用这样的相对URL。 It's much better to use the absolute url with the slash, or even better, to reverse the url instead of hardcoding it. 最好在绝对URL上加上斜杠,甚至更好地将URL反向而不是对其进行硬编码。

To fix the NoReverseMatch error on the login page, change the form tag of the template to: 要修复登录页面上的NoReverseMatch错误,请将模板的表单标签更改为:

<form method="post" action="{% url 'login' %}">

and add the name to the login url pattern. 并将名称添加到登录网址格式。

url(r'^login/$',views.login, {'template_name': 'login.html', 'authentication_form': LoginForm}, name='login'),

I would also add LOGIN_URL to your settings, 我还将LOGIN_URL添加到您的设置中,

LOGIN_URL = 'login'

and then you won't have to specify it each time you use login_required . 然后您不必每次使用login_required都指定它。

@login_required
def home(request):

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM