在项目主文件夹(第二层meiduo_mall)中创建apps包,用于管理所有的子应用,包中包括或创建 init.py文件
(meiduo_mall) PS E:\projects\meiduo_project\meiduo_mall\meiduo_mall\apps> python …\manage.py startapp users
import sys
print(sys.path)
输出列表
[‘E:\projects\meiduo_project\meiduo_mall’, ‘c:\python39\python39.zip’, ‘c:\python39\DLLs’, ‘c:\python39\lib’, ‘c:\python39’, ‘E:\projects\meiduo_project\meiduo_mall’, ‘E:\projects\meiduo_project\meiduo_mall\lib\site-packages’]
说明:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'meiduo_mall.apps.users', # 注册子应用
]
import os,sysy
BASE_DIR=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
#查看项目BASE_DIR(自我认为是跟路径)
print(BASE_DIR)
# 追加导包路径
sys.path.insert(1, os.path.join(BASE_DIR, 'apps'))
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# 'meiduo_mall.apps.users', # 注册子应用
'users', # 注册子应用
]
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>商城-注册title>
<link rel="stylesheet" type='text/css' href="{{ static('css/reset.css') }}">
<link rel="stylesheet" type='text/css' href="{{ static('css/main.css') }}">
<script type="text/javascript" src="../static/js/vue_2.6.14.min.js">script>
<script type="text/javascript" src="{{ static('js/axios_0.21.1.min.js') }}">script>
<style>
[v-cloak] {
display: none;
}
style>
head>
<body>
<div id="app">
<div class="register_con">
<div class="l_con fl">
<a href="index.html" class="reg_logo"><img src="../static/images/logo.png" alt="">a>
<div class="reg_slogan">人美货全div>
<div class="reg_banner">div>
div>
div>
<div class="r_con fr">
<div class="reg_title clearfix">
<h1>用户注册h1>
<a href="login.html">登录a>
div>
<div class="reg_form clearfix">
<form method="post" class="register_form" @submit="on_submit" v-cloak>
{{ csrf_input }}
<ul>
<li>
<label for="">用户名:label>
<input type="text" v-model="username" @blur="check_username" name="username" id="user_name">
<span class="error_tip" v-show="error_name">[[ error_name_message ]]span>
li>
<li>
<label for="">密码:label>
<input type="password" v-model="password" @blur="check_password" name="password" id="pwd">
<span class="error_tip" v-show="error_password">请输入8-20位密码span>
li>
<li>
<label for="">确认密码:label>
<input type="password" v-model="password2" @blur="check_password2" name="password2" id="pwd2">
<span class="error_tip" v-show="error_password2">两次输入的密码不一样span>
li>
<li>
<label for="">手机号:label>
<input type="text" v-model="mobile" @blur="check_mobile" name="mobile" id="phone">
<span class="error_tip" v-show="error_mobile">[[ error_mobile_message ]]span>
li>
<li>
<label for="">图形验证码:label>
<input type="text" v-model="image_code" @blur="check_image_code" name="image_code" id="msg_input">
<img v-bind:src="image_code_url" @click='generate_image_code' alt="图形验证码" class="pic_code">
<span class="error_tip" v-show="error_image_code">[[ error_image_code_message]]span>
li>
<li>
<label for="">短信验证码label>
<input type="text" v-model="sms_code" @blur="check_sms_code" name="sms_code" id="sms_code" class="msg_input">
<a @click="send_sms_code" class="get_msg_code">[[ sms_code_tip ]]a>
<span class="error_tip" v-show="error_sms_code">[[ error_sms_code_message]]span>
li>
<li class="agreement">
<input type="checkbox" v-model="allow" @change="check_allow" name="allow" id="allow">
<label for="">同意“商城用户使用协议”label>
<span class="error_tip" v-show="error_allow">请勾选用户协议span>
li>
ul>
<input type="submit" value="注册">
{% if register_errmsg %}
<span class="error_tip">{{ register_errmsg }}span>
{% endif %}
form>
div>
div>
div>
<script type="text/javascript" src="../static/js/common.js">script>
<script type="text/javascript" src="../static/js/register.js">script>
body>
html>
from django.shortcuts import render
from django.views import View
# Create your views here.
class ResgisterView(View):
""" 用户注册 """
def get(self, request):
""" 用于提供数据--用户注册页面 """
return render(request, 'register.html')
from django.contrib import admin
from django.urls import path, include
from meiduo_mall.apps.users import urls as users_urls
urlpatterns = [
path('admin/', admin.site.urls),
# users
path('', include(users_urls, namespace='users')),
]
from django.urls import path
from . import views
app_name = 'users'
urlpatterns = [
# 用户注册: reverse(users:register) == '/register/'
path('register/', views.ResgisterView.as_view(), name='register'),
]
目的:迁移建表,并以ORM面向对象的形式增删改查用户数据
from django.db import models
from django.contrib.auth.models import AbstractUser
# Create your models here.
class User(AbstractUser):
""" 自定义用户模型类 """
mobile = models.CharField(max_length=11, unique=True, verbose_name='手机号')
class Meta:
db_table = 'tb_users' # 自定义表名
verbose_name = '用户' # admin站点中显示
verbose_name_plural = verbose_name
def __str__(self) -> str:
return self.username
# 指定自定义的用户模型类,值的语法为:'z子应用.用户模型类'
AUTH_USER_MODEL = 'users.User'