django - 如何通过 django 中的视图从 html 中学习数据
问题描述
我创建了一个 html login.html
function openCity(evt, cityName) {
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
document.getElementById(cityName).style.display = "block";
evt.currentTarget.className += " active";
}
body {font-family: Arial;}
/* Style the tab */
.tab {
overflow: hidden;
border: 1px solid #ccc;
background-color: #f1f1f1;
}
/* Style the buttons inside the tab */
.tab button {
background-color: inherit;
float: left;
border: none;
outline: none;
cursor: pointer;
padding: 14px 16px;
transition: 0.3s;
font-size: 17px;
}
/* Change background color of buttons on hover */
.tab button:hover {
background-color: #ddd;
}
/* Create an active/current tablink class */
.tab button.active {
background-color: #ccc;
}
/* Style the tab content */
.tabcontent {
display: none;
padding: 6px 12px;
border: 1px solid #ccc;
border-top: none;
}
/* Full-width inputs */
input[type=text], input[type=password] {
width: 100%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
box-sizing: border-box;
}
/* Set a style for all buttons */
.tabcontent button {
background-color: #4CAF50;
color: white;
padding: 14px 20px;
margin: 8px 0;
border: none;
cursor: pointer;
width: 30%;
}
<div class="tab">
<button class="tablinks" onclick="openCity(event, 'Admin')">Admin</button>
<button class="tablinks" onclick="openCity(event, 'Facilitateur')">Facilitateur</button>
<button class="tablinks" onclick="openCity(event, 'Entrepreneur')">Entrepreneur</button>
</div>
<div id="Admin" class="tabcontent">
<form method="POST" action="">
{% csrf_token %}
<h2> Vous ètes un Admin ?</h2>
<div class="container" action >
<input type="hidden" name="user" value="1">
<label for ="id_email"><b>Votre Email</b></label>
<input id="id_email" type="text" placeholder="Entrer Votre Email" name="email" required>
<label for ="id_password" ><b>Mot de Passe</b></label>
<input id="id_password" type="password" placeholder="Entrer Votre Mot de passe" name="password" required>
<button type="submit" >Login</button>
<label>
<input type="checkbox" checked="checked" name="remember"> souviens de moi
</label>
</div>
<div class="container" style="background-color:#f1f1f1">
<button type="button" class="cancelbtn">annuler</button>
<span class="psw">oublier <a href="#">mot de passe?</a></span>
</div>
</form>
</div>
<div id="Facilitateur" class="tabcontent">
<form method="POST" action="">
{% csrf_token %}
<h2> Vous ètes un Facilitateur ?</h2>
<div class="container">
<input type="hidden" name="user" value="3">
<label for="email"><b>Votre Email</b></label>
<input type="text" placeholder="Entrer Votre Email" name="email" required>
<label for="password"><b>Mot de Passe</b></label>
<input type="password" placeholder="Entrer Votre Mot de passe" name="password" required>
<button type="submit" >Login</button>
<label>
<input type="checkbox" checked="checked" name="remember"> souviens de moi
</label>
</div>
<div class="container" style="background-color:#f1f1f1">
<button type="button" class="cancelbtn">annuler</button>
<span class="psw">Oublier <a href="#">Mot de passe?</a></span>
</div>
</form>
</div>
<div id="Entrepreneur" class="tabcontent">
<form method="post" action="">
{% csrf_token %}
<h2> Vous ètes un Entrepreneur?</h2>
<div class="container">
<input type="hidden" name="user" value="2">
<label for="email"><b>Votre Email</b></label>
<input type="text" placeholder="Entrer Votre Email" name="email" required>
<label for="psw"><b>Mot de Passe</b></label>
<input type="password" placeholder="Entrer Votre Mot de passe" name="password" required>
<button type="submit" >Login</button>
<label>
<input type="checkbox" checked="checked" name="remember"> souviens de moi
</label>
</div>
<div class="container" style="background-color:#f1f1f1">
<button type="button" class="cancelbtn">annuler</button>
<span class="psw">Oublier <a href="#">Mot de passe?</a></span>
</div>
</form>
</div>
我在 forms.py 中创建一个类来创建一个登录字段
class LoginForm(forms.ModelForm):
email = forms.EmailField()
password = forms.PasswordInput()
这是我在views.py中进行身份验证的功能
def login(request):
context = {}
user = request.user
if user.is_authenticated:
return render(request, "login.html")
if request.POST:
form = LoginForm(request.POST)
if form.is_valid():
email = request.POST['email']
password = request.POST['password']
user = authenticate(request, email=email, password=password)
if user:
login(request, user)
if CustomUser.user_type == '1':
return render(request, "administrateur.html")
elif CustomUser.user_type == '2':
return render(request, "entrepreneur.html")
else:
form = AuthenticationForm()
context['login_form'] = form
return render(request, "login.html", context)
我有两种类型的用户企业家和管理员
当我将电子邮件和密码放入 html 并提交时,它留在同一页面(login.html)我认为(也许)这是 html 中的一个简单问题,我不知道如何发送和学习从 html 到视图
解决方案
我在您的login
视图中看到了问题
if CustomUser.user_type == '1':
return render(request, "administrateur.html")
elif CustomUser.user_type == '2':
return render(request, "entrepreneur.html")
在这里,您需要检查实例属性而不是检查模型的属性,即。user_type
喜欢以下
if user.user_type == '1':
return render(request, "administrateur.html")
elif user.user_type == '2':
return render(request, "entrepreneur.html")
推荐阅读
- r - 从 R 中的浏览器/用户代理数据检测设备(移动设备、平板电脑和 PC)
- excel - 如何更新用户表单中的条目?
- javascript - 有没有办法让我在这个函数中封装异步/等待处理?
- python - 在另一个视图函数中包含一个带有渲染的函数
- node.js - 你应该在同一个端口上创建 Express 和 React 吗?
- javascript - 加载包含数组的 URL/API 的问题 - javascript/p5js
- c# - 使用实体进行数据验证
- mysql - 创建多个可以访问个人数据库的用户
- c++ - 如何在数组中获取某些值?
- html - 使用 Latex 和 HTML 渲染在 R Markdown 中标记矩阵的行和列