简体   繁体   中英

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! the others applications still working normally 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'),
]

Your login_url is missing a leading slash. That means that you will redirected from /log/ to /log/login/ , which will give a 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. It's much better to use the absolute url with the slash, or even better, to reverse the url instead of hardcoding it.

To fix the NoReverseMatch error on the login page, change the form tag of the template to:

<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'

and then you won't have to specify it each time you use login_required .

@login_required
def home(request):

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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