在這邊我是基於Macpython
項目建立以前咱們新建一個數據庫, 下面是操做步驟mysql
xxzxdeMacBook-Pro:bin xxzx$ /usr/local/mysql/bin/mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 114
Server version: 8.0.12 MySQL Community Server - GPL
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| bj18 |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.10 sec)
mysql> create database dailyfresh charset=utf8;
Query OK, 1 row affected, 1 warning (0.09 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| bj18 |
| dailyfresh |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
6 rows in set (0.01 sec)
mysql> use dailyfresh;
Database changed
複製代碼
要注意的是,若是咱們鏈接的不是本地的數據庫, 咱們須要作一些特殊配置. 這裏我使用本機作一下操做git
在 PyCharm 中也能夠經過終端操做, 在這裏咱們經過PyCharm內的終端打開sql
xxzxdeMacBook-Pro:bin xxzx$ cd ~/Desktop/python/bj18/
xxzxdeMacBook-Pro:bj18 xxzx$ django-admin startproject dailyfresh
xxzxdeMacBook-Pro:bj18 xxzx$ cd dailyfresh/
xxzxdeMacBook-Pro:dailyfresh xxzx$ ls
dailyfresh manage.py
xxzxdeMacBook-Pro:dailyfresh xxzx$ python3 manage.py startapp user
xxzxdeMacBook-Pro:dailyfresh xxzx$ python3 manage.py startapp goods
xxzxdeMacBook-Pro:dailyfresh xxzx$ python3 manage.py startapp cart
xxzxdeMacBook-Pro:dailyfresh xxzx$ python3 manage.py startapp order
xxzxdeMacBook-Pro:dailyfresh xxzx$ ls
cart dailyfresh goods manage.py order user
複製代碼
通常狀況下, 項目的應用比較多,咱們會在項目中新建一個 package 把全部的應用放到這個文件夾下面數據庫
若是咱們將來方便,不想寫如 apps.user 這種 咱們能夠本身定義搜索路徑,以下django
其中的 BASE_DIR 至關於當前項目的目錄 直接拼接便可,這樣咱們在下面 INSTALLED_APPS 中直接輸入便可,以下bash
以後咱們作一些基礎配置,templates, 新增文件夾微信
配置靜態文件路徑session
在每一個應用中添加一個urls.py文件app
新建一個 package 的 db 文件夾做爲抽象基類
在當前項目中須要用到 tinymce 這個安裝包 咱們能夠在 PyCharm 的底部找到 Terminal 以下圖
在這裏安裝, 會自動安裝到當前環境中, 這這裏咱們須要作以下配置
根據咱們分析頁面中數據庫表中
分別在每一個應用中新建模型,具體代碼以下
from django.db import models
from django.contrib.auth.models import AbstractUser
from db.base_model import BaseModel
# Create your models here.
class User(AbstractUser, BaseModel):
'''用戶模型類'''
class Meta:
db_table = 'df_user'
verbose_name = '用戶'
verbose_name_plural = verbose_name
class Address(BaseModel):
'''地址模型類'''
user = models.ForeignKey('User', verbose_name='所屬帳戶')
receiver = models.CharField(max_length=20, verbose_name='收件人')
addr = models.CharField(max_length=256, verbose_name='收件地址')
zip_code = models.CharField(max_length=6, null=True, verbose_name='郵政編碼')
phone = models.CharField(max_length=11, verbose_name='聯繫電話')
is_default = models.BooleanField(default=False, verbose_name='是否默認')
class Meta:
db_table = 'df_address'
verbose_name = '地址'
verbose_name_plural = verbose_name
複製代碼
from django.db import models
from db.base_model import BaseModel
from tinymce.models import HTMLField
# Create your models here.
class GoodsType(BaseModel):
'''商品類型模型類'''
name = models.CharField(max_length=20, verbose_name='種類名稱')
logo = models.CharField(max_length=20, verbose_name='標識')
image = models.ImageField(upload_to='type', verbose_name='商品類型圖片')
class Meta:
db_table = 'df_goods_type'
verbose_name = '商品種類'
verbose_name_plural = verbose_name
def __str__(self):
return self.name
class GoodsSKU(BaseModel):
'''商品SKU模型類'''
status_choices = (
(0, '下線'),
(1, '上線'),
)
type = models.ForeignKey('GoodsType', verbose_name='商品種類')
goods = models.ForeignKey('Goods', verbose_name='商品SPU')
name = models.CharField(max_length=20, verbose_name='商品名稱')
desc = models.CharField(max_length=256, verbose_name='商品簡介')
price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='商品價格')
unite = models.CharField(max_length=20, verbose_name='商品單位')
image = models.ImageField(upload_to='goods', verbose_name='商品圖片')
stock = models.IntegerField(default=1, verbose_name='商品庫存')
sales = models.IntegerField(default=0, verbose_name='商品銷量')
status = models.SmallIntegerField(default=1, choices=status_choices, verbose_name='商品狀態')
class Meta:
db_table = 'df_goods_sku'
verbose_name = '商品'
verbose_name_plural = verbose_name
class Goods(BaseModel):
'''商品SPU模型類'''
name = models.CharField(max_length=20, verbose_name='商品SPU名稱')
# 富文本類型:帶有格式的文本
detail = HTMLField(blank=True, verbose_name='商品詳情')
class Meta:
db_table = 'df_goods'
verbose_name = '商品SPU'
verbose_name_plural = verbose_name
class GoodsImage(BaseModel):
'''商品圖片模型類'''
sku = models.ForeignKey('GoodsSKU', verbose_name='商品')
image = models.ImageField(upload_to='goods', verbose_name='圖片路徑')
class Meta:
db_table = 'df_goods_image'
verbose_name = '商品圖片'
verbose_name_plural = verbose_name
class IndexGoodsBanner(BaseModel):
'''首頁輪播商品展現模型類'''
sku = models.ForeignKey('GoodsSKU', verbose_name='商品')
image = models.ImageField(upload_to='banner', verbose_name='圖片')
index = models.SmallIntegerField(default=0, verbose_name='展現順序')
class Meta:
db_table = 'df_index_banner'
verbose_name = '首頁輪播商品'
verbose_name_plural = verbose_name
class IndexTypeGoodsBanner(BaseModel):
'''首頁分類商品展現模型類'''
DISPLAY_TYPE_CHOICES = (
(0, "標題"),
(1, "圖片")
)
type = models.ForeignKey('GoodsType', verbose_name='商品類型')
sku = models.ForeignKey('GoodsSKU', verbose_name='商品SKU')
display_type = models.SmallIntegerField(default=1, choices=DISPLAY_TYPE_CHOICES, verbose_name='展現類型')
index = models.SmallIntegerField(default=0, verbose_name='展現順序')
class Meta:
db_table = 'df_index_type_goods'
verbose_name = "主頁分類展現商品"
verbose_name_plural = verbose_name
class IndexPromotionBanner(BaseModel):
'''首頁促銷活動模型類'''
name = models.CharField(max_length=20, verbose_name='活動名稱')
url = models.URLField(verbose_name='活動連接')
image = models.ImageField(upload_to='banner', verbose_name='活動圖片')
index = models.SmallIntegerField(default=0, verbose_name='展現順序')
class Meta:
db_table = 'df_index_promotion'
verbose_name = "主頁促銷活動"
verbose_name_plural = verbose_name
複製代碼
from django.db import models
from db.base_model import BaseModel
# Create your models here.
class OrderInfo(BaseModel):
'''訂單模型類'''
PAY_METHOD_CHOICES = (
(1, '貨到付款'),
(2, '微信支付'),
(3, '支付寶'),
(4, '銀聯支付')
)
ORDER_STATUS_CHOICES = (
(1, '待支付'),
(2, '待發貨'),
(3, '待收貨'),
(4, '待評價'),
(5, '已完成')
)
order_id = models.CharField(max_length=128, primary_key=True, verbose_name='訂單id')
user = models.ForeignKey('user.User', verbose_name='用戶')
addr = models.ForeignKey('user.Address', verbose_name='地址')
pay_method = models.SmallIntegerField(choices=PAY_METHOD_CHOICES, default=3, verbose_name='支付方式')
total_count = models.IntegerField(default=1, verbose_name='商品數量')
total_price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='商品總價')
transit_price = models.DecimalField(max_digits=10, decimal_places=2,verbose_name='訂單運費')
order_status = models.SmallIntegerField(choices=ORDER_STATUS_CHOICES, default=1, verbose_name='訂單狀態')
trade_no = models.CharField(max_length=128, verbose_name='支付編號')
class Meta:
db_table = 'df_order_info'
verbose_name = '訂單'
verbose_name_plural = verbose_name
class OrderGoods(BaseModel):
'''訂單商品模型類'''
order = models.ForeignKey('OrderInfo', verbose_name='訂單')
sku = models.ForeignKey('goods.GoodsSKU', verbose_name='商品SKU')
count = models.IntegerField(default=1, verbose_name='商品數目')
price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='商品價格')
comment = models.CharField(max_length=256, verbose_name='評論')
class Meta:
db_table = 'df_order_goods'
verbose_name = '訂單商品'
verbose_name_plural = verbose_name
複製代碼
from django.db import models
from db.base_model import BaseModel
# Create your models here.
class OrderInfo(BaseModel):
'''訂單模型類'''
PAY_METHOD_CHOICES = (
(1, '貨到付款'),
(2, '微信支付'),
(3, '支付寶'),
(4, '銀聯支付')
)
ORDER_STATUS_CHOICES = (
(1, '待支付'),
(2, '待發貨'),
(3, '待收貨'),
(4, '待評價'),
(5, '已完成')
)
order_id = models.CharField(max_length=128, primary_key=True, verbose_name='訂單id')
user = models.ForeignKey('user.User', verbose_name='用戶')
addr = models.ForeignKey('user.Address', verbose_name='地址')
pay_method = models.SmallIntegerField(choices=PAY_METHOD_CHOICES, default=3, verbose_name='支付方式')
total_count = models.IntegerField(default=1, verbose_name='商品數量')
total_price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='商品總價')
transit_price = models.DecimalField(max_digits=10, decimal_places=2,verbose_name='訂單運費')
order_status = models.SmallIntegerField(choices=ORDER_STATUS_CHOICES, default=1, verbose_name='訂單狀態')
trade_no = models.CharField(max_length=128, verbose_name='支付編號')
class Meta:
db_table = 'df_order_info'
verbose_name = '訂單'
verbose_name_plural = verbose_name
class OrderGoods(BaseModel):
'''訂單商品模型類'''
order = models.ForeignKey('OrderInfo', verbose_name='訂單')
sku = models.ForeignKey('goods.GoodsSKU', verbose_name='商品SKU')
count = models.IntegerField(default=1, verbose_name='商品數目')
price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='商品價格')
comment = models.CharField(max_length=256, verbose_name='評論')
class Meta:
db_table = 'df_order_goods'
verbose_name = '訂單商品'
verbose_name_plural = verbose_name
複製代碼
接下來咱們指定當前django系統使用的模型類
當咱們使用
xxzxdeMacBook-Pro:dailyfresh xxzx$ python manage.py create superuser
這條命令去建立用戶的時候, django 會使用本身默認的模型類爲咱們建立帳戶具體帳戶信息以下 下面是例子代碼
mysql> show tables;
+----------------------------+
| Tables_in_bj18 |
+----------------------------+
| auth_group |
| auth_group_permissions |
| auth_permission |
| auth_user |
| auth_user_groups |
| auth_user_user_permissions |
| booktest_bookinfo |
| booktest_heroinfo |
| django_admin_log |
| django_content_type |
| django_migrations |
| django_session |
+----------------------------+
12 rows in set (0.00 sec)
mysql>
複製代碼
咱們會看到有一個 auth_user 這個表, 會有一個默認的 auth_user_model. 若是咱們不指定的話, 就是使用這個默認的model, 因此咱們在使用 python manage.py create superuser 命令去建立內容的時候, 就會默認放到 auth_user 這個表裏, 因此咱們須要使用以下方式指定使用的模型類
mysql> desc auth_user;
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| password | varchar(128) | NO | | NULL | |
| last_login | datetime(6) | YES | | NULL | |
| is_superuser | tinyint(1) | NO | | NULL | |
| username | varchar(30) | NO | UNI | NULL | |
| first_name | varchar(30) | NO | | NULL | |
| last_name | varchar(30) | NO | | NULL | |
| email | varchar(254) | NO | | NULL | |
| is_staff | tinyint(1) | NO | | NULL | |
| is_active | tinyint(1) | NO | | NULL | |
| date_joined | datetime(6) | NO | | NULL | |
+--------------+--------------+------+-----+---------+----------------+
11 rows in set (0.03 sec)
複製代碼
下面記錄一下遇到的問題
django.core.exceptions.ImproperlyConfigured: Specifying a namespace in include() without providing an app_name is not supported. Set the app_name attribute in the included module, or pass a 2-tuple containing the list of patterns and app_name instead.