zoukankan      html  css  js  c++  java
  • 项目的初始化结构

    1.项目的初始化结构

    img

    img

    2.创建django项目

    root@dev:shiyanlou_project# workon syl
    
    (syl) root@dev:shiyanlou_project# cd /aaa/shiyanlou_project/
    
    (syl) root@dev:shiyanlou_project# django-admin startproject syl
    
    # 注:创建三个python包, apps、utils、libs
    

    3.创建user模型

    3.1创建用户模型user

    python ../manage.py startapp user # 创建user模型
    
    
    

    3.2 在setting.py中注册user模型(第一步)

    INSTALLED_APPS = [
    
    'user.apps.UserConfig',
    
    ]
    
    # 注:pycharm无法联想,需要把apps文件夹设置为源根
    
    
    

    3.3 syl/urls.py添加主路由(第二步)

    urlpatterns = [
    
    path('user/', include('user.urls')),
    
    ]
    
    
    

    3.4 创建 user/urls.py子路由文件(第三步)

    from django.urls import path
    
    urlpatterns = [
    
    ]
    
    
    

    3.5 syl/settings.py中添加apps路径

    # 把apps目录插入,到系统包搜索路径第一个位置中
    
    sys.path.insert(0,os.path.join(BASE_DIR,'apps'))
    

    4.重写Django默认认证用户模型

    4.1 user/models.py创建模型:从写django user表

    from django.db import models
    
    from django.contrib.auth.models import AbstractUser
    
    # Create your models here.
    
    class User(AbstractUser):
    
    	phone = models.CharField('手机号',max_length=20)
    
    	img = models.ImageField(upload_to='user',null=True)
    
    	nick_name = models.CharField('昵称',max_length=20)
    
    	address = models.CharField('地址',max_length=255)
    
    	class Meta:
    
    		db_table = 'tb_user'
    
    
    

    4.2 syl/settings.py中注册自己的用户模型类

    # 注册自己的用户模型类: 应用名.模型名,指定我们重写的User表进行身份验证
    
    AUTH_USER_MODEL = 'user.User
    
    
    

    4.3 配置mysql

    1.创建mysql数据库
    
    	mysql> create database syldb charset utf8;
    
    2.在syl/settings.py中配置mysql'''
    
        DATABASES = {
        'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'syldb', # 指定数据库名称:MyCRM
        'USER': 'root',
        'PASSWORD': '1',
        'HOST': '127.0.0.1',
        'PORT': '3306',
        }
    	}
    
     
    

    4.4生成表

    python manage.py makemigrations
    python manage.py migrate
    
  • 相关阅读:
    LeetCode 516. Longest Palindromic Subsequence
    LeetCode 432. All O`one Data Structure
    LeetCode 450. Delete Node in a BST
    LeetCode 460. LFU Cache
    LeetCode 341. Flatten Nested List Iterator
    LeetCode 381. Insert Delete GetRandom O(1)
    LeetCode 380. Insert Delete GetRandom O(1)
    LintCode Coins in a Line III
    LintCode Coins in a Line II
    LintCode Coins in a Line
  • 原文地址:https://www.cnblogs.com/lxs1030/p/13746938.html
Copyright © 2011-2022 走看看