[英]Upon login redirection is not happening django
On my localhost (127.0.0.1:8000) i have configured a Login page and upon login its not redirecting to "Automation page (form_name_view". 在我的本地主机(127.0.0.1:8000)上,我已经配置了一个登录页面,并且在登录时它没有重定向到“自动化页面(form_name_view)”。
Note:- when i click login button nothing it happening whether with right credentials or wrong. 注意:-当我单击登录按钮时,无论使用正确的凭据还是错误,都不会发生。
In views.py 在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
In Models.py 在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
In admin.py 在admin.py中
from django.contrib import admin
from first_app.models import Login
# Register your models here.
admin.site.register(Login)
In login.html 在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>
In urls.py 在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')),
]
What i want is:- A) only the user whos username is in db should be able to access the Automation page(form_name_view) 我想要的是:-A)只有用户名在db中的用户才能访问自动化页面(form_name_view)
B) when the user logs-In and successful then the must be redirected to Automation page(form_name_view) otherwise the page says and error message and stays on login page. B)当用户登录并成功登录时,则必须将其重定向到自动化页面(form_name_view),否则该页面会显示错误消息并停留在登录页面上。
thnx in advance 提前thnx
You need to pay attention to the case of your strings and variables. 您需要注意字符串和变量的大小写。 POST
in django is capitalised: django中的POST
大写:
if request.method == 'POST'
if request.method == 'POST'
username = request.POST['username']
username = request.POST['username']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.