[英]Upon login redirection is not happening django
在我的本地主機(127.0.0.1:8000)上,我已經配置了一個登錄頁面,並且在登錄時它沒有重定向到“自動化頁面(form_name_view)”。
注意:-當我單擊登錄按鈕時,無論使用正確的憑據還是錯誤,都不會發生。
在views.py中
from django.contrib.auth import authenticate,login,logout
# Create your views here.
def login_view(request):
context = {}
if request.method == "post":
username = request.post['username']
password = request.post['password']
user = authenticate(request, username=username, password=password)
if user:
login(request, user)
return HttpResponseRedirect(reverse('IP form'))
else:
context["error"] = "Provide valid credentials"
return render (request,"first_app/login.html", context)
else:
return render (request,"first_app/login.html", context)
def form_name_view(request): #this is the view to which i want to redirect
if request.method == "POST":
#some code
在Models.py中
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Login(models.Model):
username = models.CharField(max_length=50,)
password = models.CharField(max_length=32,)
def __str__(self):
return self.user.Username
在admin.py中
from django.contrib import admin
from first_app.models import Login
# Register your models here.
admin.site.register(Login)
在login.html中
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Login</title>
</head>
<body>
<h1> Please Login</h1>
<form method="post">
{% csrf_token %}
<table>
<tr>
<td><label for="username">Enter username: </label></td>
<td><input type="text" name="username" id="username" required></td>
</tr>
<tr>
<td><label for="username">Enter password: </label></td>
<td><input type="password" name="password" id="password" required></td>
</tr>
<p> {{ error }} </p>
</table>
<input type="submit" value="LOGIN"/>
</form>
</body>
</html>
在urls.py中
from django.contrib import admin
from django.urls import path
from django.conf.urls import include
from first_app import views
urlpatterns = [
path('Automation_page/', views.form_name_view,name='IP form'),
path('admin/', admin.site.urls),
path('',views.login_view,name='login'),
path('first_app/',include('first_app.urls')),
]
我想要的是:-A)只有用戶名在db中的用戶才能訪問自動化頁面(form_name_view)
B)當用戶登錄並成功登錄時,則必須將其重定向到自動化頁面(form_name_view),否則該頁面會顯示錯誤消息並停留在登錄頁面上。
提前thnx
您需要注意字符串和變量的大小寫。 django中的POST
大寫:
if request.method == 'POST'
username = request.POST['username']
您是否嘗試閱讀這些文檔 ? 登錄后,您也可以嘗試LOGIN_REDIRECT_URL ='/ home / dashboard'導航到您選擇的頁面。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.