我刚开始编写 Python Django 代码。我只是想做一个基本的登录项目。我试图将登录变量从 HTML 端传递到文件views.py。
这是我的 HTML 代码:
<form action="/index" method="post">
{% csrf_token %}
<div class="form-group">
<label for="username">Username</label>
<input class="form-control" id="username" type="email"/>
</div>
<div class="form-group">
<div class="d-flex justify-content-between">
<label for="password">Password</label><a class="fs--1">Forgot Password?</a>
</div>
<input class="form-control" id="password" type="password" />
</div>
<div class="custom-control custom-checkbox">
<input class="custom-control-input" type="checkbox" id="card-checkbox" checked="checked"/>
<label class="custom-control-label" for="card-checkbox">Remember me</label>
</div>
<div class="form-group">
<button class="btn btn-primary btn-block mt-3"
type="submit"
id= "login"
name="submit">Log in</button>
</div>
</form>
我的urls.py文件:
urlpatterns = [
path('', views.loginPage, name="login"),
path('index', views.home, name="index")
]
我的forms.py文件:
from django import forms
class LoginForm(forms.Form):
username = forms.EmailField(label="Username"),
password = forms.CharField(label="Password")
还有我的views.py文件:
def home(request):
context = {}
if request.method == 'POST':
form = LoginForm(request.POST)
print('form:', form)
if form.is_valid():
print('niceee')
else:
return render(request, 'index.html', context)
else:
form = LoginForm()
我只想在views.py 中查看用户名和密码值。但是,我无法在views.py 中看到变量。我怎样才能解决这个问题?
def home(request):
if request.method == 'POST':
form = LoginForm(request.POST)
print('form:', form)
if form.is_valid():
print('username:', form.cleaned_data.get('username'))
print('password:', form.cleaned_data.get('password'))
print('niceee')
else:
form = LoginForm()
context = {}
return render(request, 'index.html', context)
检查服务器的用户名和密码值。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句