使用DJango实现注册模型 用户名,插件可使用TextInput 密码,插件可使用PasswordInput 确认密码,插件可使用PasswordInput 性别,插件可使用RadioSelect 邮箱,插件可使用EmailInput 电话,插件可使用NumberInput,注:电话号码11位 (2)登录模型 用户名 密码 2. 前端页面设计 (1)注册页面(用户名、密码、确认密码、性别、邮箱、电话) 注册成功提示成功信息,失败提示失败原因 (2)登陆页面(用户名、密码) 登陆成功显示登录成功,登陆失败提示失败原因要views,html,models,form,url等所有有关代码
时间: 2024-03-21 20:42:31 浏览: 75
Django实现登录-注册等功能
4星 · 用户满意度95%
下面是使用Django实现注册和登录模型以及前端页面设计的代码:
models.py
```python
from django.db import models
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
gender_choices = (
('M', 'Male'),
('F', 'Female'),
('O', 'Other')
)
phone_number = models.CharField(max_length=11)
gender = models.CharField(max_length=1, choices=gender_choices)
email = models.EmailField(unique=True)
```
forms.py
```python
from django import forms
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
from .models import User
class RegisterForm(UserCreationForm):
gender_choices = (
('M', 'Male'),
('F', 'Female'),
('O', 'Other')
)
email = forms.EmailField(required=True, help_text='Required.')
phone_number = forms.CharField(max_length=11, required=True, help_text='Required. 11 digits.')
gender = forms.ChoiceField(choices=gender_choices, widget=forms.RadioSelect)
class Meta:
model = User
fields = ['username', 'email', 'phone_number', 'gender', 'password1', 'password2']
class LoginForm(AuthenticationForm):
class Meta:
model = User
fields = ['username', 'password']
```
views.py
```python
from django.shortcuts import render, redirect
from django.contrib.auth import login, authenticate
from .forms import RegisterForm, LoginForm
def register(request):
if request.method == 'POST':
form = RegisterForm(request.POST)
if form.is_valid():
user = form.save()
user.refresh_from_db()
user.email = form.cleaned_data.get('email')
user.phone_number = form.cleaned_data.get('phone_number')
user.gender = form.cleaned_data.get('gender')
user.save()
raw_password = form.cleaned_data.get('password1')
user = authenticate(username=user.username, password=raw_password)
login(request, user)
return redirect('home')
else:
form = RegisterForm()
return render(request, 'registration/register.html', {'form': form})
def login(request):
if request.method == 'POST':
form = LoginForm(request.POST)
if form.is_valid():
username = form.cleaned_data.get('username')
password = form.cleaned_data.get('password')
user = authenticate(username=username, password=password)
if user is not None:
login(request, user)
return redirect('home')
else:
form.add_error(None, 'Invalid username or password.')
else:
form = LoginForm()
return render(request, 'registration/login.html', {'form': form})
```
urls.py
```python
from django.urls import path
from .views import register, login
urlpatterns = [
path('register/', register, name='register'),
path('login/', login, name='login'),
]
```
register.html
```html
{% extends 'base.html' %}
{% block content %}
<h2>Register</h2>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Register</button>
</form>
{% endblock %}
```
login.html
```html
{% extends 'base.html' %}
{% block content %}
<h2>Login</h2>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Login</button>
</form>
{% endblock %}
```
注意事项:
1. 在使用UserCreationForm创建用户时,默认只包含username和password字段,需要创建自定义User模型并继承AbstractUser,并在其中添加其他字段。
2. 在使用ModelForm创建表单时,如果需要添加额外的字段,可以在Meta类中定义fields属性。
3. 在注册视图中,需要在form.save()之后手动更新User模型的额外字段,否则它们将为空。
4. 在登录视图中,需要使用AuthenticationForm验证表单,并使用authenticate验证用户名和密码,如果验证成功,则使用login函数登录用户。
阅读全文