即使密码不正确,用户也会在django中验证并通过身份验证检查



我正在使用landsatxplore API下载卫星图像,现在我正在Django中使用相同的API创建一个web应用程序来下载卫星图像。

在简单的python脚本中。

from landsatxplore.api import API
username = 'username'
password = 'password'
api = API(username, password)

如果用户名和密码正确,它将创建一个API实例/密钥,如果用户名和口令不正确,它会抛出一个异常:

landsatxplore.errors.USGSAuthenticationError: AUTH_INVALID: User credential verification failed.

我在Django中尝试了同样的逻辑,但即使我键入了错误的用户名和密码,它也会通过身份验证,并将我发送到下一个页面homepage.html,只有在用户名和密码正确的情况下才能访问该页面。

views.py

from landsatxplore import errors
from django.shortcuts import render,redirect
from django.contrib import messages
from landsatxplore.api import API

# Create your views here.
def index(request):
username = request.POST.get('username')
password = request.POST.get('password')
if username == None or password == None:
messages.info(request,"please Enter the username and password")
else:
try:
# Initialize a new API instance and get an access key   
api = API(username, password)
except errors.USGSUnauthorizedError:
messages.info(request,"Username and password is wrong")
else:
return render(request,"homepage.html")
return render(request, 'index.html')
def homepage(request):
return render(request,'homepage.html')

HTML:

{% for message in messages %}
<p>{{message}}</p>
{% endfor %}
<form method ="POST" action = "homepage">
{% csrf_token %}
<p>Username:</p>
<input type = "text" name ="username">
<p>Password:</p>
<input type="text" name = "password">
<br>
<input type="submit" value = "Submit">

</form>

新视图.py

def login(request):
if request.method == "POST":
username = request.POST.get('username')
password = request.POST.get('password')

if not username or not password:
return redirect('/')
else:
try:
# Initialize a new API instance and get an access key   
api = API(str(username), str(password))
except errors.USGSAuthenticationError:
messages.info(request,"Username and password is wrong")
return render(request,"register.html")
else:
return render(request,"homepage.html")
return render(request,"login.html")

相关内容

最新更新