django中 结合Cookie,实现注册、登录和注销功能 的html代码
时间: 2024-02-15 09:05:17 浏览: 39
以下是结合Cookie实现注册、登录和注销功能的HTML代码示例:
注册页面(register.html):
```
<!DOCTYPE html>
<html>
<head>
<title>注册</title>
</head>
<body>
<h1>注册</h1>
<form method="post" action="{% url 'register' %}">
{% csrf_token %}
<label>用户名:</label>
<input type="text" name="username"><br><br>
<label>密码:</label>
<input type="password" name="password"><br><br>
<input type="submit" value="注册">
</form>
</body>
</html>
```
登录页面(login.html):
```
<!DOCTYPE html>
<html>
<head>
<title>登录</title>
</head>
<body>
<h1>登录</h1>
<form method="post" action="{% url 'login' %}">
{% csrf_token %}
<label>用户名:</label>
<input type="text" name="username"><br><br>
<label>密码:</label>
<input type="password" name="password"><br><br>
<input type="submit" value="登录">
</form>
</body>
</html>
```
注销页面(logout.html):
```
<!DOCTYPE html>
<html>
<head>
<title>注销</title>
</head>
<body>
<h1>注销</h1>
<p>确定要注销吗?</p>
<form method="post" action="{% url 'logout' %}">
{% csrf_token %}
<input type="submit" value="是">
</form>
</body>
</html>
```
其中,`{% url 'register' %}`、`{% url 'login' %}`和`{% url 'logout' %}`是Django中的URL模板标签,用于生成对应的URL。在视图函数中,需要设置cookie,可以使用`HttpResponse`的`set_cookie`方法。在登录成功后,可以将用户信息存储在cookie中,以便下次访问时自动登录。在注销时,需要删除cookie中的用户信息。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)