用layui写注册登录html,django写注册登录的代码并且连接mysql
时间: 2024-01-29 21:02:39 浏览: 80
django实例手册.pdf
首先,我们需要创建一个Django项目并创建一个应用程序。我将应用程序命名为“accounts”。
接下来,我们需要安装MySQL数据库并创建一个数据库。我将数据库命名为“accounts_db”。
然后,我们需要安装python的MySQL驱动程序,以便Django可以与MySQL数据库进行通信。在命令行中运行以下命令安装mysqlclient:
```
pip install mysqlclient
```
接下来,在Django项目的settings.py文件中,在DATABASES字典中添加以下内容:
```python
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'accounts_db',
'USER': 'your_username',
'PASSWORD': 'your_password',
'HOST': 'localhost',
'PORT': '3306',
}
}
```
现在,我们可以创建一个模型来定义用户。在accounts应用程序的models.py文件中添加以下内容:
```python
from django.db import models
class User(models.Model):
username = models.CharField(max_length=50)
password = models.CharField(max_length=50)
email = models.EmailField(max_length=100)
```
接下来,我们需要创建一个表来存储用户数据。在命令行中运行以下命令:
```
python manage.py makemigrations
python manage.py migrate
```
我们需要在accounts应用程序中创建一个forms.py文件,并添加以下内容:
```python
from django import forms
class UserRegistrationForm(forms.Form):
username = forms.CharField(max_length=50)
password = forms.CharField(max_length=50, widget=forms.PasswordInput)
email = forms.EmailField(max_length=100)
```
接下来,我们需要在accounts应用程序中创建一个views.py文件,并添加以下内容:
```python
from django.shortcuts import render, redirect
from .forms import UserRegistrationForm
from .models import User
def register(request):
if request.method == 'POST':
form = UserRegistrationForm(request.POST)
if form.is_valid():
username = form.cleaned_data['username']
password = form.cleaned_data['password']
email = form.cleaned_data['email']
user = User(username=username, password=password, email=email)
user.save()
return redirect('login')
else:
form = UserRegistrationForm()
return render(request, 'register.html', {'form': form})
def login(request):
if request.method == 'POST':
username = request.POST['username']
password = request.POST['password']
try:
user = User.objects.get(username=username, password=password)
return redirect('home')
except User.DoesNotExist:
pass
return render(request, 'login.html')
```
最后,我们需要在accounts应用程序的urls.py文件中添加以下内容:
```python
from django.urls import path
from . import views
urlpatterns = [
path('register/', views.register, name='register'),
path('login/', views.login, name='login'),
]
```
现在,我们可以创建一个register.html模板来显示用户注册表单,并创建一个login.html模板来显示用户登录表单。我使用layui框架编写了这些模板。以下是register.html模板的代码:
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Register</title>
<link rel="stylesheet" href="https://cdn.staticfile.org/layui/2.5.6/css/layui.min.css">
</head>
<body>
<div class="layui-container">
<div class="layui-row">
<div class="layui-col-md-offset3 layui-col-md6">
<form class="layui-form" method="post">
{% csrf_token %}
<div class="layui-form-item">
<label class="layui-form-label">Username</label>
<div class="layui-input-block">
{{ form.username }}
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">Password</label>
<div class="layui-input-block">
{{ form.password }}
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">Email</label>
<div class="layui-input-block">
{{ form.email }}
</div>
</div>
<div class="layui-form-item">
<div class="layui-input-block">
<button class="layui-btn" lay-submit lay-filter="register">Register</button>
</div>
</div>
</form>
</div>
</div>
</div>
<script src="https://cdn.staticfile.org/layui/2.5.6/layui.min.js"></script>
</body>
</html>
```
以下是login.html模板的代码:
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login</title>
<link rel="stylesheet" href="https://cdn.staticfile.org/layui/2.5.6/css/layui.min.css">
</head>
<body>
<div class="layui-container">
<div class="layui-row">
<div class="layui-col-md-offset3 layui-col-md6">
<form class="layui-form" method="post">
{% csrf_token %}
<div class="layui-form-item">
<label class="layui-form-label">Username</label>
<div class="layui-input-block">
<input type="text" name="username" required lay-verify="required" placeholder="Please input your username" autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">Password</label>
<div class="layui-input-block">
<input type="password" name="password" required lay-verify="required" placeholder="Please input your password" autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<div class="layui-input-block">
<button class="layui-btn" lay-submit lay-filter="login">Login</button>
</div>
</div>
</form>
</div>
</div>
</div>
<script src="https://cdn.staticfile.org/layui/2.5.6/layui.min.js"></script>
</body>
</html>
```
现在我们可以运行Django开发服务器,并通过浏览器访问注册和登录页面。
阅读全文