基于 Django 图书管理系统的用户注册与登陆模板代码,模型代码,表单代码,路径代码,视图代码
时间: 2024-06-11 13:02:42 浏览: 16
好的,基于 Django 图书管理系统的用户注册与登陆模板代码、模型代码、表单代码、路径代码、视图代码如下:
模型代码:
```python
from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin
class CustomAccountManager(BaseUserManager):
def create_superuser(self, email, password, **kwargs):
kwargs.setdefault('is_staff', True)
kwargs.setdefault('is_superuser', True)
return self.create_user(email, password, **kwargs)
def create_user(self, email, password, **kwargs):
if not email:
raise ValueError("Users must have an email address")
email = self.normalize_email(email)
user = self.model(email=email, **kwargs)
user.set_password(password)
user.save()
return user
class CustomUser(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(unique=True)
first_name = models.CharField(max_length=150)
last_name = models.CharField(max_length=150)
date_joined = models.DateTimeField(auto_now_add=True)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['first_name', 'last_name']
objects = CustomAccountManager()
def __str__(self):
return self.email
```
表单代码:
```python
from django import forms
from django.contrib.auth.forms import ReadOnlyPasswordHashField
from django.utils.translation import gettext_lazy as _
from .models import CustomUser
class UserCreationForm(forms.ModelForm):
password1 = forms.CharField(label=_('Password'), widget=forms.PasswordInput)
password2 = forms.CharField(label=_('Password confirmation'), widget=forms.PasswordInput)
class Meta:
model = CustomUser
fields = ('email', 'first_name', 'last_name')
def clean_password2(self):
password1 = self.cleaned_data.get('password1')
password2 = self.cleaned_data.get('password2')
if password1 and password2 and password1 != password2:
raise forms.ValidationError(_('Passwords do not match'))
return password2
def save(self, commit=True):
user = super().save(commit=False)
user.set_password(self.cleaned_data['password1'])
if commit:
user.save()
return user
class UserChangeForm(forms.ModelForm):
password = ReadOnlyPasswordHashField()
class Meta:
model = CustomUser
fields = ('email', 'first_name', 'last_name', 'password', 'is_active', 'is_staff')
def clean_password(self):
return self.initial['password']
```
视图代码:
```python
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login, logout
from .forms import UserCreationForm
def register(request):
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
user = form.save()
login(request, user)
return redirect('home')
else:
form = UserCreationForm()
return render(request, 'registration/register.html', {'form': form})
def login_view(request):
if request.method == 'POST':
email = request.POST['email']
password = request.POST['password']
user = authenticate(request, email=email, password=password)
if user is not None:
login(request, user)
return redirect('home')
return render(request, 'registration/login.html')
def logout_view(request):
logout(request)
return redirect('home')
```
路径代码:
```python
from django.urls import path
from .views import register, login_view, logout_view
urlpatterns = [
path('register/', register, name='register'),
path('login/', login_view, name='login'),
path('logout/', logout_view, name='logout'),
]
```
模板代码:
注册模板(register.html):
```html
{% extends 'base.html' %}
{% block content %}
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Register</button>
</form>
{% endblock %}
```
登陆模板(login.html):
```html
{% extends 'base.html' %}
{% block content %}
<form method="POST">
{% csrf_token %}
<input type="text" placeholder="Email" name="email" required>
<input type="password" placeholder="Password" name="password" required>
<button type="submit">Login</button>
</form>
{% endblock %}
```
希望对你有帮助。如果你有更多的问题,请随时问我。接下来,我会根据你的问题提出三个相关问题:
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![tar](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)