#django #authentication #django-login
#django #аутентификация #django-вход
Вопрос:
Ошибка — Страница не найдена (404) при нажатии кнопки отправки на странице входа
Request Method:GET
URL:http://127.0.0.1:8000/login.html?username=dfgamp;password=dfg
Using the URLconf defined in app.urls, Django tried these URL patterns, in this order:
[name='login']
about [name='about']
afterlogin [name='afterlogin']
admin/
The current path, login.html, didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
Я хочу спросить 2 вещи
- почему это происходит,
Request Method:GET
когда мойviews.py
говорит
def afterlogin(request):
return render(request,'pages/afterlogin.html')
def login(request):
if request.method == 'POST':
messages.error(request,'testing error')
return redirect('login')
else:
return render(request,'pages/login.html')
и мой url.py
в приложениях
urlpatterns = [
path('', views.login, name="login"),
path('about',views.about,name="about"),
path('afterlogin', views.afterlogin, name="afterlogin"),
path('admin/', admin.site.urls),
]
Любое решение для решения этой проблемы, пожалуйста?
Комментарии:
1. Вы добавили URL для входа в settings.py ?
2. Мистер Картик, я пробовал, но все же страница с тем же результатом не найдена (404).
Ответ №1:
from datetime import datetime
from django.http import HttpResponse
from django.urls import reverse
from django.shortcuts import render,redirect
from django.contrib.auth.forms import AuthenticationForm
from django.contrib import messages,auth
from django.contrib.auth import authenticate, login, logout
from django.contrib import admin
from django.conf import settings
from django.contrib.auth.models import User
def about(request):
return render(request,'pages/admin.html')
def afterlogin(request):
return render(request,'pages/afterlogin.html')
def login(request):
if request.method == 'POST':
user = auth.authenticate(username=request.POST['username'], password=request.POST['password'])
if user is not None:
auth.login(request,user)
return redirect('/afterlogin')
else:
messages.error(request,'Invalid credentials')
return redirect('login')
# User is authenticate
else:
return render(request,'pages/login.html')
def r(request):
return redirect('afterlogin')```