您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“Python中Django設計的示例分析”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“Python中Django設計的示例分析”這篇文章吧。
user-用戶管理
curser-課程管理
organization-機構和教師管理
修改users目錄下的models
# _*_ encoding:utf-8 _*_ from __future__ import unicode_literals from datetime import datetime from django.db import models from django.contrib.auth.models import AbstractUser # Create your models here. class UserProfile(AbstractUser): # verbose_name:備注 # blank:是否可以為空 # nick_name = models.CharField(max_length=50,verbose_name=u"昵稱",default="") birthday = models.DateField(verbose_name=u"生日",blank=True) gender = models.CharField(choices=(('male',u'男'),('female',u'女')),default="female",max_length=6) address = models.CharField(max_length=100,default=u"") mobile = models.CharField(max_length=11,null=True,blank=True) image = models.ImageField(upload_to="image/%Y/%m",default=u"image/default.png",max_length=100) class Meta: verbose_name = u"用戶信息" verbose_name_plural = verbose_name def __unicode__(self): return self.username class EmailVerifyRecord(models.Model): code = models.CharField(max_length=20,verbose_name=u"驗證碼") email = models.EmailField(max_length=50,verbose_name=u"郵箱") send_type = models.CharField(max_length=10,choices=(("register",u"注冊"),("forget",u"找回密碼")),) send_time = models.DateTimeField(default=datetime.now(),verbose_name=u"添加時間") class Meta: verbose_name = u"郵箱驗證碼" verbose_name_plural = verbose_name class Banner(models.Model): title = models.CharField(max_length=100,verbose_name=u"標題") image = models.ImageField(max_length=100,upload_to="banner/%Y/%m",verbose_name=u"輪播圖") url = models.URLField(max_length=100,verbose_name=u"訪問地址") index = models.IntegerField(verbose_name=u"順序",default=100) add_time = models.DateTimeField(default=datetime.now(),verbose_name=u"添加時間") class Meta: verbose_name = u"輪播圖" verbose_name_plural = verbose_name
修改配置文件setting.py中的INSTALLED_APPS,將我們的app users加入,添加AUTH_USER_MODEL = “users.UserProfile”
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'users', ] AUTH_USER_MODEL = "users.UserProfile"
運行python manage.py runserver 0.0.0.0:9000
解決問題:
pip3 install Pillow(安裝Pillow包)
python manage.py makemigrations users
python manage.py migrate users
解決users.0001_initial,將配置文件中的INSTALLED_APPS中的django.contrib.admin注釋掉
修改course 目錄下的models
# _*_ encoding:utf-8 _*_ from __future__ import unicode_literals from datetime import datetime from django.db import models # Create your models here. class Course(models.Model): name = models.CharField(max_length=50,verbose_name=u"課程名") desc = models.CharField(max_length=300,verbose_name=u"課程描述") detail = models.TextField(verbose_name=u"課程詳情") degree = models.CharField(max_length=2,choices=(('cj','初級'),('zj','中級'),('gj','高級'))) learn_time = models.IntegerField(default=0,verbose_name=u"學習時長") students = models.IntegerField(default=0,verbose_name=u"學習人數") fav_nums = models.IntegerField(default=0,verbose_name=u"收藏人數") image = models.ImageField(max_length=100,verbose_name="封面圖片",upload_to="courses/%Y/%m") click_nums = models.IntegerField(default=0,verbose_name=u"點擊數") add_time = models.DateTimeField(default=datetime.now,verbose_name=u"添加時間") class Meta: verbose_name = u"課程" verbose_name_plural = verbose_name class Lesson(models.Model): course = models.ForeignKey(Course,verbose_name=u"課程",on_delete=models.CASCADE) name = models.CharField(max_length=100,verbose_name=u"章節名") add_time = models.DateTimeField(default=datetime.now, verbose_name=u"添加時間") class Meta: verbose_name = u"章節" verbose_name_plural = verbose_name class Video(models.Model): lesson = models.ForeignKey(Lesson,verbose_name=u"章節",on_delete=models.CASCADE) name = models.CharField(max_length=100, verbose_name=u"視頻名") add_time = models.DateTimeField(default=datetime.now, verbose_name=u"添加時間") class Meta: verbose_name = u"視頻" verbose_name_plural = verbose_name class CourseResource(models.Model): course = models.ForeignKey(Course, verbose_name=u"課程",on_delete=models.CASCADE) name = models.CharField(max_length=100, verbose_name=u"名稱") download = models.FileField(upload_to="course/recourse/%Y/%m",verbose_name=u"資源文件", max_length=100) add_time = models.DateTimeField(default=datetime.now, verbose_name=u"添加時間") class Meta: verbose_name = u"課程資源" verbose_name_plural = verbose_name
修改配置文件setting.py中的INSTALLED_APPS,將我們的app course加入
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'users', 'course', ]
運行python manage.py runserver 0.0.0.0:9000
原因
在django2.0后,定義外鍵和一對一關系的時候需要加on_delete選項,此參數為了避免兩個表里的數據不一致問題,不然會報錯:
TypeError: init() missing 1 required positional argument: ‘on_delete'
涉及到外鍵都需要改成
course = models.ForeignKey(Course,verbose_name=u"課程",on_delete=models.CASCADE)
修改organization目錄下的models
# _*_ encoding:utf-8 _*_ from __future__ import unicode_literals from datetime import datetime from django.db import models # Create your models here. class CityDict(models.Model): name = models.CharField(max_length=20, verbose_name=u"城市名稱") desc = models.CharField(max_length=200,verbose_name=u"城市描述") add_time = models.DateTimeField(default=datetime.now, verbose_name=u"添加時間") class Meta: verbose_name = u"城市" verbose_name_plural = verbose_name class CourseOrg(models.Model): name = models.CharField(max_length=50,verbose_name=u"機構名稱") desc = models.TextField(verbose_name=u"機構描述") click_nums = models.IntegerField(default=0,verbose_name=u"點擊數") fav_nums = models.IntegerField(default=0,verbose_name=u"收藏數") image = models.ImageField(upload_to="org/%Y/%m",verbose_name=u"封面圖",max_length=100) address = models.CharField(max_length=150,verbose_name=u"機構地址") city = models.ForeignKey(CityDict,verbose_name=u"所在城市",on_delete=models.CASCADE) add_time = models.DateTimeField(default=datetime.now, verbose_name=u"添加時間") class Meta: verbose_name = u"課程機構" verbose_name_plural = verbose_name class Teacher(models.Model): org = models.ForeignKey(CourseOrg,verbose_name=u"所屬機構",on_delete=models.CASCADE) name = models.CharField(max_length=50, verbose_name=u"教師名稱") work_years = models.IntegerField(default=0,verbose_name=u"工作年限") work_company = models.CharField(max_length=50,verbose_name=u"就職公司") work_position = models.CharField(max_length=50,verbose_name=u"公司職位") points = models.CharField(max_length=50,verbose_name=u"教學特點") click_nums = models.IntegerField(default=0, verbose_name=u"點擊數") fav_nums = models.IntegerField(default=0, verbose_name=u"收藏數") add_time = models.DateTimeField(default=datetime.now, verbose_name=u"添加時間") class Meta: verbose_name = u"教師" verbose_name_plural = verbose_name
修改配置文件setting.py中的INSTALLED_APPS,將我們的app organization加入
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'users', 'course', 'organization', ]
# _*_ encoding:utf-8 _*_from __future__ import unicode_literalsfrom datetime import datetimefrom django.db import modelsfrom users.models import UserProfilefrom course.models import Course# Create your models here.class UserAsk(models.Model): name = models.CharField(max_length=20,verbose_name=u"姓名") mobile = models.CharField(max_length=11,verbose_name=u"手機") course_name = models.CharField(max_length=50,verbose_name=u"課程名") add_time = models.DateTimeField(default=datetime.now,verbose_name=u"添加時間") class Meta: verbose_name = u"用戶咨詢" verbose_name_plural = verbose_nameclass CourseComments(models.Model): # 課程評論 user = models.ForeignKey(UserProfile,verbose_name=u"用戶",on_delete=models.CASCADE) course = models.ForeignKey(Course,verbose_name=u"課程",on_delete=models.CASCADE) comments = models.CharField(max_length=200,verbose_name=u"評論") add_time = models.DateTimeField(default=datetime.now,verbose_name=u"評論時間") class Meta: verbose_name = u"課程評論" verbose_name_plural = verbose_nameclass UserFavorite(models.Model): user = models.ForeignKey(UserProfile,verbose_name=u"用戶",on_delete=models.CASCADE) fav_id = models.IntegerField(default=0,verbose_name=u"數據id") fav_type = models.IntegerField(choices=((1,'課程'),(2,'課程機構'),(3,'講師')),default=1, verbose_name=u"收藏類型") add_time = models.DateTimeField(default=datetime.now, verbose_name=u"收藏時間") class Meta: verbose_name = u"用戶收藏" verbose_name_plural = verbose_nameclass UserMessage(models.Model): user = models.ForeignKey(UserProfile, verbose_name=u"接收用戶",on_delete=models.CASCADE) message = models.CharField(max_length=500,verbose_name=u"消息內容") has_read = models.BooleanField(default=False,verbose_name=u"是否已讀") add_time = models.DateTimeField(default=datetime.now,verbose_name=u"添加時間") class Meta: verbose_name = u"用戶信息" verbose_name_plural = verbose_nameclass UserCourse(models.Model): user = models.ForeignKey(UserProfile, verbose_name=u"用戶",on_delete=models.CASCADE) course = models.ForeignKey(Course, verbose_name=u"課程",on_delete=models.CASCADE) add_time = models.DateTimeField(default=datetime.now, verbose_name=u"添加時間") class Meta: verbose_name = u"用戶課程" verbose_name_plural = verbose_name
修改配置文件setting.py中的INSTALLED_APPS,將我們的app operation加入
# _*_ encoding:utf-8 _*_ from __future__ import unicode_literals from datetime import datetime from django.db import models from users.models import UserProfile from course.models import Course # Create your models here. class UserAsk(models.Model): name = models.CharField(max_length=20,verbose_name=u"姓名") mobile = models.CharField(max_length=11,verbose_name=u"手機") course_name = models.CharField(max_length=50,verbose_name=u"課程名") add_time = models.DateTimeField(default=datetime.now,verbose_name=u"添加時間") class Meta: verbose_name = u"用戶咨詢" verbose_name_plural = verbose_name class CourseComments(models.Model): # 課程評論 user = models.ForeignKey(UserProfile,verbose_name=u"用戶",on_delete=models.CASCADE) course = models.ForeignKey(Course,verbose_name=u"課程",on_delete=models.CASCADE) comments = models.CharField(max_length=200,verbose_name=u"評論") add_time = models.DateTimeField(default=datetime.now,verbose_name=u"評論時間") class Meta: verbose_name = u"課程評論" verbose_name_plural = verbose_name class UserFavorite(models.Model): user = models.ForeignKey(UserProfile,verbose_name=u"用戶",on_delete=models.CASCADE) fav_id = models.IntegerField(default=0,verbose_name=u"數據id") fav_type = models.IntegerField(choices=((1,'課程'),(2,'課程機構'),(3,'講師')),default=1, verbose_name=u"收藏類型") add_time = models.DateTimeField(default=datetime.now, verbose_name=u"收藏時間") class Meta: verbose_name = u"用戶收藏" verbose_name_plural = verbose_name class UserMessage(models.Model): user = models.ForeignKey(UserProfile, verbose_name=u"接收用戶",on_delete=models.CASCADE) message = models.CharField(max_length=500,verbose_name=u"消息內容") has_read = models.BooleanField(default=False,verbose_name=u"是否已讀") add_time = models.DateTimeField(default=datetime.now,verbose_name=u"添加時間") class Meta: verbose_name = u"用戶信息" verbose_name_plural = verbose_name class UserCourse(models.Model): user = models.ForeignKey(UserProfile, verbose_name=u"用戶",on_delete=models.CASCADE) course = models.ForeignKey(Course, verbose_name=u"課程",on_delete=models.CASCADE) add_time = models.DateTimeField(default=datetime.now, verbose_name=u"添加時間") class Meta: verbose_name = u"用戶課程" verbose_name_plural = verbose_name
將4個app統一放到apps包下
因為app太多,不好管理,所以統一放到apps目錄下進行管理
將app路徑寫入到配置文件setting.py中,防止包導入失敗
import os import sys # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0,os.path.join(BASE_DIR,'apps'))
python manage.py makemigrations
python manage.py migrate
生成數據表
> 運行python manage.py runserver 0.0.0.0:9000
以上是“Python中Django設計的示例分析”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。