使用django開發一個上線標準的mooc網站(四)

本節開始實戰搭建慕課網站

課程框架

  1. django app設計
  2. 各app models設計
  3. 數據表生成與修改

django APP設計:

  • users-用戶管理
  • course-課程管理
  • organization-機構和教師管理
  • operation-用戶操作管理

1.搭建開發環境

(1)添加虛擬環境

Microsoft Windows [版本 6.1.7601]版權所有 (c) 2009 Microsoft Corporation。保留所有權利。C:UsersAdministrator>mkvirtualenv mxonlineNew python executable in C:UsersAdministratorEnvsmxonlineScriptspython.exeInstalling setuptools, pip, wheel...done.(mxonline) C:UsersAdministrator>pip install django==1.9Collecting django==1.9 Downloading Django-1.9-py2.py3-none-any.whl (6.6MB) 100% |████████████████████████████████| 6.6MB 180kB/sInstalling collected packages: djangoSuccessfully installed django-1.9(mxonline) C:UsersAdministrator>

(2)使用pycharm新建項目MxOnline

(3)安裝mysql-python驅動

訪問連接Python Extension Packages for Windows

下載對應的版本:

(我的是win7 64位,所以選mysql_python-1.2.5-cp27-none-win_amd64.whl)

安裝使用:

(mxonline) C:UsersAdministratorPycharmProjects>pip install MySQL_python-1.2.5-cp27-none-win_amd64.whlProcessing c:usersadministratorpycharmprojectsmysql_python-1.2.5-cp27-none-win_amd64.whlInstalling collected packages: MySQL-pythonSuccessfully installed MySQL-python-1.2.5(mxonline) C:UsersAdministratorPycharmProjects>

(4)配置資料庫

使用Navicat新建資料庫mxonline

在項目中配置資料庫連接

MxOnlinesettings.py

DATABASES = { default: { ENGINE: django.db.backends.mysql, NAME: "testdjango", USER: "root", PASSWORD: "root", HOST: "127.0.0.1", }}

生成django默認的數據表:

出現報錯:

解決:

在MxOnlinesettings.py最後添加

STATIC_ROOT = os.path.join(BASE_DIR, "static/")

生成django默認的數據表

anage.py@MxOnline > makemigrations"C:Program FilesJetBrainsPyCharm 2017.3.1in
unnerw.exe" C:UsersAdministratorEnvsmxonlineScriptspython.exe "C:Program FilesJetBrainsPyCharm 2017.3.1helperspycharmdjango_manage.py" makemigrations C:/Users/Administrator/PycharmProjects/MxOnlineNo changes detectedProcess finished with exit code 0manage.py@MxOnline > migrate"C:Program FilesJetBrainsPyCharm 2017.3.1in
unnerw.exe" C:UsersAdministratorEnvsmxonlineScriptspython.exe "C:Program FilesJetBrainsPyCharm 2017.3.1helperspycharmdjango_manage.py" migrate C:/Users/Administrator/PycharmProjects/MxOnlineOperations to perform: Apply all migrations: admin, contenttypes, auth, sessionsRunning migrations: Rendering model states... DONE Applying contenttypes.0001_initial... OK Applying auth.0001_initial... OK Applying admin.0001_initial... OK Applying admin.0002_logentry_remove_auto_add... OK Applying contenttypes.0002_remove_content_type_name... OK Applying auth.0002_alter_permission_name_max_length... OK Applying auth.0003_alter_user_email_max_length... OK Applying auth.0004_alter_user_username_opts... OK Applying auth.0005_alter_user_last_login_null... OK Applying auth.0006_require_contenttypes_0002... OK Applying auth.0007_alter_validators_add_error_messages... OK Applying sessions.0001_initial... OKProcess finished with exit code 0

(5)debug運行測試

2.開發users應用(users的models設計)

(1)新建users的app

manage.py@MxOnline > startapp users"C:Program FilesJetBrainsPyCharm 2017.3.1in
unnerw.exe" C:UsersAdministratorEnvsmxonlineScriptspython.exe "C:Program FilesJetBrainsPyCharm 2017.3.1helperspycharmdjango_manage.py" startapp users C:/Users/Administrator/PycharmProjects/MxOnline
Following files were affected C:UsersAdministratorPycharmProjectsMxOnlineusers ests.pyC:UsersAdministratorPycharmProjectsMxOnlineusersmodels.pyC:UsersAdministratorPycharmProjectsMxOnlineusersadmin.pyC:UsersAdministratorPycharmProjectsMxOnlineusers\__init__.pyC:UsersAdministratorPycharmProjectsMxOnlineusersmigrations\__init__.pyC:UsersAdministratorPycharmProjectsMxOnlineusersapps.pyC:UsersAdministratorPycharmProjectsMxOnlineusersviews.pyProcess finished with exit code 0

(2)編寫models.py

from __future__ import unicode_literalsfrom django.db import modelsfrom django.contrib.auth.models import AbstractUser# Create your models here.class UserProfile(AbstractUser): nick_name = models.CharField(max_length=50, verbose_name=u"昵稱", default="") birthday = models.DateField(verbose_name=u"生日", null=True, blank=True) gender = models.CharField(choices=(("male",u"男"),("female",u"女")), default="female") 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): # 不重載的話,print實例時,不列印自定義的字元串 return self.username

修改settings.py

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"

(3)不斷調試

運行,報錯:

File "C:UsersAdministratorPycharmProjectsMxOnlineusersmodels.py", line 11SyntaxError: Non-ASCII character xe6 in file C:UsersAdministratorPycharmProjectsMxOnlineusersmodels.py on line 11, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

解決:

在models.py中添加:

# _*_ encoding:utf-8 _*_

再次運行,仍報錯:

ERRORS:users.UserProfile.gender: (fields.E120) CharFields must define a max_length attribute.users.UserProfile.image: (fields.E210) Cannot use ImageField because Pillow is not installed. HINT: Get Pillow at https://pypi.python.org/pypi/Pillow or run command "pip install Pillow".System check identified 2 issues (0 silenced).

按照提示修改:

gender = models.CharField(max_length=5, choices=(("male", u"男"), ("female", u"女")), default="female")

安裝Pillow:

(mxonline) C:UsersAdministrator>pip install PillowCollecting Pillow Using cached Pillow-4.3.0-cp27-cp27m-win_amd64.whlCollecting olefile (from Pillow) Using cached olefile-0.44.zipBuilding wheels for collected packages: olefile Running setup.py bdist_wheel for olefile ... done Stored in directory: C:UsersAdministratorAppDataLocalpipCachewheels205849cc7bd00345397059149a10b0259ef38b867935ea2ecff99a9bSuccessfully built olefileInstalling collected packages: olefile, PillowSuccessfully installed Pillow-4.3.0 olefile-0.44(mxonline) C:UsersAdministrator>

均解決後,仍報錯:

Unhandled exception in thread started by <function wrapper at 0x0000000003A2D5F8>Traceback (most recent call last): ... File "C:UsersAdministratorEnvsmxonlinelibsite-packagesdjangodbmigrationsloader.py", line 160, in check_key raise ValueError("Dependency on app with no migrations: %s" % key[0])ValueError: Dependency on app with no migrations: users

發現是沒有生成users表,解決:

manage.py@MxOnline > makemigrations users"C:Program FilesJetBrainsPyCharm 2017.3.1in
unnerw.exe" C:UsersAdministratorEnvsmxonlineScriptspython.exe "C:Program FilesJetBrainsPyCharm 2017.3.1helperspycharmdjango_manage.py" makemigrations users C:/Users/Administrator/PycharmProjects/MxOnline
Migrations for users: 0001_initial.py: - Create model UserProfileFollowing files were affected C:UsersAdministratorPycharmProjectsMxOnlineusersmigrations001_initial.pyProcess finished with exit code 0manage.py@MxOnline > migrate users"C:Program FilesJetBrainsPyCharm 2017.3.1in
unnerw.exe" C:UsersAdministratorEnvsmxonlineScriptspython.exe "C:Program FilesJetBrainsPyCharm 2017.3.1helperspycharmdjango_manage.py" migrate users C:/Users/Administrator/PycharmProjects/MxOnline
Operations to perform: Apply all migrations: usersRunning migrations: Rendering model states... DONE Applying users.0001_initial... OKThe following content types are stale and need to be deleted: auth | userAny objects related to these content types by a foreign key will alsobe deleted. Are you sure you want to delete these content types?If youre unsure, answer no. Type yes to continue, or no to cancel: yesProcess finished with exit code 0

檢查生成情況:

運行:

3.繼續完善app models結構

可能出現循環import

資料庫設計

  • django app設計
  • user models.py編寫
  • courses models.py編寫
  • organization models.py編寫
  • operation models.pu編寫

解決方法:app model分層

(1)users models.py

自定義userprofile覆蓋默認user表

EmailVerifyRecord-郵箱驗證碼

PageBanner-輪播圖

# _*_ encoding:utf-8 _*_from __future__ import unicode_literalsfrom datetime import datetimefrom django.db import modelsfrom django.contrib.auth.models import AbstractUser# Create your models here.class UserProfile(AbstractUser): nick_name = models.CharField(max_length=50, verbose_name=u"昵稱", default="") birthday = models.DateField(verbose_name=u"生日", null=True, blank=True) gender = models.CharField(max_length=5, choices=(("male", u"男"), ("female", u"女")), default="female") 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): # 不重載的話,print實例時,不列印自定義的字元串 return self.usernameclass 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(choices=(("register",u"註冊"),("forget",u"找回密碼")),max_length=10) send_time = models.DateTimeField(default=datetime.now) # 注意一定要把datetime.now後邊的括弧去掉,否則帶括弧會獲取model編譯的時間,不帶括弧才會獲取model實例化的時間。 class Meta: verbose_name = u"郵箱驗證碼" verbose_name_plural = verbose_nameclass Banner(models.Model): title = models.CharField(max_length=100, verbose_name=u"標題") image = models.ImageField(upload_to="banner/%Y/%m", verbose_name=u"輪播圖", max_length=100) url = models.URLField(max_length=200, verbose_name=u"訪問地址") index = models.IntegerField(default=100, verbose_name=u"順序") add_time = models.DateTimeField(default=datetime.now, verbose_name=u"添加時間" class Meta: verbose_name = u"輪播圖" verbose_name_plural = verbose_name

(2)繼續進行course app models的編寫

新建courses app

manage.py@MxOnline > startapp courses"C:Program FilesJetBrainsPyCharm 2017.3.1in
unnerw.exe" C:UsersAdministratorEnvsmxonlineScriptspython.exe "C:Program FilesJetBrainsPyCharm 2017.3.1helperspycharmdjango_manage.py" startapp courses C:/Users/Administrator/PycharmProjects/MxOnline
Following files were affected C:UsersAdministratorPycharmProjectsMxOnlinecoursesmodels.pyC:UsersAdministratorPycharmProjectsMxOnlinecoursesapps.pyC:UsersAdministratorPycharmProjectsMxOnlinecourses ests.pyC:UsersAdministratorPycharmProjectsMxOnlinecoursesadmin.pyC:UsersAdministratorPycharmProjectsMxOnlinecoursesviews.pyC:UsersAdministratorPycharmProjectsMxOnlinecoursesmigrations\__init__.pyC:UsersAdministratorPycharmProjectsMxOnlinecourses\__init__.pyProcess finished with exit code 0

根據頁面分析courses的表結構:

  • Course-課程基本信息
  • Lesson-章節信息
  • Video-視頻
  • CourseResource-課程資源

編寫代碼:

coursesmodels.py

# _*_ encoding:utf-8 _*_from __future__ import unicode_literalsfrom datetime import datetimefrom 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(choices=(("cj",u"初級"),("zj",u"中級"),("gj",u"高級")), max_length=2) learn_times = 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(upload_to="course/%Y/%m", verbose_name=u"封面圖", max_length=100) 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_nameclass Lesson(models.Model): course = models.ForeignKey(Course, verbose_name=u"課程" 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_nameclass Video(models.Model): lesson = models.ForeignKey(Lesson,verbose_name=u"章節") 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_nameclass CourseResource(models.Model): course = models.ForeignKey(Course, verbose_name=u"課程") name = models.CharField(max_length=100, verbose_name=u"名稱") download = models.FileField(upload_to="course/resource/%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

(3)繼續進行organization app models的編寫

manage.py@MxOnline > startapp organization"C:Program FilesJetBrainsPyCharm 2017.3.1in
unnerw.exe" C:UsersAdministratorEnvsmxonlineScriptspython.exe "C:Program FilesJetBrainsPyCharm 2017.3.1helperspycharmdjango_manage.py" startapp organization C:/Users/Administrator/PycharmProjects/MxOnline
Following files were affected C:UsersAdministratorPycharmProjectsMxOnlineorganizationmodels.pyC:UsersAdministratorPycharmProjectsMxOnlineorganizationviews.pyC:UsersAdministratorPycharmProjectsMxOnlineorganizationmigrations\__init__.pyC:UsersAdministratorPycharmProjectsMxOnlineorganization\__init__.pyC:UsersAdministratorPycharmProjectsMxOnlineorganization ests.pyC:UsersAdministratorPycharmProjectsMxOnlineorganizationapps.pyC:UsersAdministratorPycharmProjectsMxOnlineorganizationadmin.pyProcess finished with exit code 0

organization models.py

  • CourseOrg-課程機構基本信息
  • Teacher-教師基本信息
  • CityDict-城市信息

# _*_ encoding:utf-8 _*_from __future__ import unicode_literalsfrom datetime import datetimefrom 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) class Meta: verbose_name = u"城市" verbose_name_plural = verbose_nameclass CourseOrg(models.Model): name = models.CharField(max_length=50, verbose_name=u"機構名稱") desc = models.TextField(verbose_name=u"機構描述") click_num = models.IntegerField(default=0, verbose_name=u"點擊數") fav_num = 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"所在城市") add_time = models.DateTimeField(default=datetime.now) class Meta: verbose_name = u"課程機構" verbose_name_plural = verbose_nameclass Teacher(models.Model): org = models.ForeignKey(CourseOrg, verbose_name=u"所屬機構") 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_num = models.IntegerField(default=0, verbose_name=u"點擊數") fav_num = models.IntegerField(default=0, verbose_name=u"收藏數") add_time = models.DateTimeField(default=datetime.now) class Meta: verbose_name = u"教師" verbose_name_plural = verbose_name

(4)繼續進行operation app models的編寫

manage.py@MxOnline > startapp operation"C:Program FilesJetBrainsPyCharm 2017.3.1in
unnerw.exe" C:UsersAdministratorEnvsmxonlineScriptspython.exe "C:Program FilesJetBrainsPyCharm 2017.3.1helperspycharmdjango_manage.py" startapp operation C:/Users/Administrator/PycharmProjects/MxOnline
Following files were affected C:UsersAdministratorPycharmProjectsMxOnlineoperationmigrations\__init__.pyC:UsersAdministratorPycharmProjectsMxOnlineoperationadmin.pyC:UsersAdministratorPycharmProjectsMxOnlineoperation\__init__.pyC:UsersAdministratorPycharmProjectsMxOnlineoperation ests.pyC:UsersAdministratorPycharmProjectsMxOnlineoperationviews.pyC:UsersAdministratorPycharmProjectsMxOnlineoperationmodels.pyC:UsersAdministratorPycharmProjectsMxOnlineoperationapps.pyProcess finished with exit code 0

operation models.py

  • UserAsk - 用戶諮詢
  • CourseComments - 用戶評論
  • UserFavorite - 用戶收藏
  • UserMessage - 用戶消息
  • UserCourse - 用戶學習的課程

# _*_ encoding:utf-8 _*_from __future__ import unicode_literalsfrom datetime import datetimefrom django.db import modelsfrom users.models import UserProfilefrom courses.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"用戶") course = models.ForeignKey(Course, verbose_name=u"課程") comment = 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"用戶") 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.IntegerField(default=0, verbose_name=u"接收用戶") 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"用戶") course = models.ForeignKey(Course, verbose_name=u"課程") add_time = models.DateTimeField(default=datetime.now, verbose_name=u"添加時間") class Meta: verbose_name = u"用戶課程" verbose_name_plural = verbose_name

(5)在settings.py中註冊上面建立的app

(這樣就的通過django的makemigration生成對應的表)

INSTALLED_APPS = [ django.contrib.admin, django.contrib.auth, django.contrib.contenttypes, django.contrib.sessions, django.contrib.messages, django.contrib.staticfiles, users, courses, organization, operation ]

通過剛才設計的資料庫,生成對應的表:

manage.py@MxOnline > makemigrations"C:Program FilesJetBrainsPyCharm 2017.3.1in
unnerw.exe" C:UsersAdministratorEnvsmxonlineScriptspython.exe "C:Program FilesJetBrainsPyCharm 2017.3.1helperspycharmdjango_manage.py" makemigrations C:/Users/Administrator/PycharmProjects/MxOnlineMigrations for courses: 0001_initial.py: - Create model Course - Create model CourseResource - Create model Lesson - Create model VideoMigrations for operation: 0001_initial.py: - Create model CourseComments - Create model UserAsk - Create model UserCourse - Create model UserFavorite - Create model UserMessageMigrations for users: 0002_banner_emailverifyrecord.py: - Create model Banner - Create model EmailVerifyRecordMigrations for organization: 0001_initial.py: - Create model CityDict - Create model CourseOrg - Create model TeacherFollowing files were affected C:UsersAdministratorPycharmProjectsMxOnlinecoursesmigrations0001_initial.pyC:UsersAdministratorPycharmProjectsMxOnlineorganizationmigrations0001_initial.pyC:UsersAdministratorPycharmProjectsMxOnlineusersmigrations0002_banner_emailverifyrecord.pyC:UsersAdministratorPycharmProjectsMxOnlineoperationmigrations0001_initial.pyProcess finished with exit code 0manage.py@MxOnline > migrate"C:Program FilesJetBrainsPyCharm 2017.3.1in
unnerw.exe" C:UsersAdministratorEnvsmxonlineScriptspython.exe "C:Program FilesJetBrainsPyCharm 2017.3.1helperspycharmdjango_manage.py" migrate C:/Users/Administrator/PycharmProjects/MxOnlineOperations to perform: Apply all migrations: users, sessions, admin, auth, courses, contenttypes, organization, operationRunning migrations: Rendering model states... DONE Applying courses.0001_initial... OK Applying operation.0001_initial... OK Applying organization.0001_initial... OK Applying users.0002_banner_emailverifyrecord... OKProcess finished with exit code 0

(6)將app放到統一的目錄中:

新建package並命名為apps,用於統一存放app:

將剛才生成的4個app拖到apps中:

注意下面的兩個選項的對勾一定都要去掉,否則pycharm會改動你的源碼。

注意觀察確定後源碼的變化:

發現引用下面會出現紅線,因為路徑變化了,pycharm找不到users了,解決方法:

apps文件夾右鍵->Mark Directory as->Sources Root:

再次觀察同樣位置:

發現紅線消失了,這是告訴pycharm將該目錄作為Sources 目錄,系統會在該目錄查找。但是這樣還不夠,如果在命令行中運行,仍會報錯:

(mxonline) C:UsersAdministratorPycharmProjectsMxOnline>python manage.py runserverTraceback (most recent call last): File "manage.py", line 10, in <module> execute_from_command_line(sys.argv) File "C:UsersAdministratorEnvsmxonlinelibsite-packagesdjangocoremanagement\__init__.py", line 350, in execute_from_command_line utility.execute() File "C:UsersAdministratorEnvsmxonlinelibsite-packagesdjangocoremanagement\__init__.py", line 342, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "C:UsersAdministratorEnvsmxonlinelibsite-packagesdjangocoremanagement\__init__.py", line 176, in fetch_command commands = get_commands() File "C:UsersAdministratorEnvsmxonlinelibsite-packagesdjangoutilslru_cache.py", line 100, in wrapper result = user_function(*args, **kwds) File "C:UsersAdministratorEnvsmxonlinelibsite-packagesdjangocoremanagement\__init__.py", line 71, in get_commands for app_config in reversed(list(apps.get_app_configs())): File "C:UsersAdministratorEnvsmxonlinelibsite-packagesdjangoapps
egistry.py", line 137, in get_app_configs
self.check_apps_ready() File "C:UsersAdministratorEnvsmxonlinelibsite-packagesdjangoapps
egistry.py", line 124, in check_apps_ready
raise AppRegistryNotReady("Apps arent loaded yet.")django.core.exceptions.AppRegistryNotReady: Apps arent loaded yet.(mxonline) C:UsersAdministratorPycharmProjectsMxOnline>

解決方法:將apps加入到搜索目錄之下(在settings.py中增加以下代碼):

import osimport 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))

debug運行測試:

命令行運行測試:

注意一開始的報錯是因為沒有加入系統路徑:

sys.path.insert(0,os.path.join(BASE_DIR, apps))

最後git並上傳

$ git initInitialized empty Git repository in C:/Users/Administrator/PycharmProjects/MxOnline/.git/$ git add .$ git commit -m "first commit"$ git remote add origin https://github.com/lilyef2000/mxonline.git$ git push -u origin masterCounting objects: 34, done.Delta compression using up to 4 threads.Compressing objects: 100% (32/32), done.Writing objects: 100% (34/34), 15.98 KiB | 2.00 MiB/s, done.Total 34 (delta 3), reused 0 (delta 0)remote: Resolving deltas: 100% (3/3), done.To https://github.com/lilyef2000/mxonline.git 2028cdd..c9b6cb8 master -> masterBranch master set up to track remote branch master from origin.

或使用pycharm默認的git功能:

在Version Control->GitHub菜單中輸入自己的用戶名、口令:

在VCS->Git->add,添加修改項:

VCS->commit

填寫Commit Message後,選擇commit and push:

上傳完成。


推薦閱讀:

Django中提示TemplateDoesNotExist?
用Django學習設計網站後台有什麼好書可以入門和深入學習?
Django 1.6 下模板怎麼用?
Django 學習順序及入門要求?

TAG:Django框架 | Python | MOOC学院 |