程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Building Django background management system based on Python

編輯:Python

A good project database management background is necessary , Today I will teach you how to build Django Website backstage , Okay , Start to get to the point

Today, I will take building a personal blog website as an example to explain the construction of the website background


One 、 The creation of blog website

  1. Create project

  2. Build site (sites)Model, The first of these two steps is an introduction , This is direct operation

Two 、 Database configuration

  1. To introduce the database configuration, you need to setting Configure the linked database , Here the system and configuration have been completed , Link to a db.sqlite3 The database of , Maybe some readers will ask , Where is this database , Why didn't I , No problem , You run a project , The system will automatically generate a database , Of course django Also support MySQL Wait for other databases , For the convenience of explanation , Just use the system's own

  2. INSTALLED_APPS This parameter is the installation myBlog All used in this project Application, Of course we created sites Also a Application, Add in everything , The following are the frameworks of the system , Default installation

    • django.contrib.admin —— Manage site

    • django.contrib.auth —— Authentication system

    • django.contrib.contenttypes —— Framework for content types

    • django.contrib.sessions —— Conversational framework

    • django.contrib.messages —— Message frame

    • django.contrib.staticfiles —— Manage static file framework

  3. After adding sites To INSTALLED_APPS After that, you need to execute the following terminal commands , migrate Will view INSTALLED_APPS Set up , And according to myBlog/settings.py The database settings in the file create any necessary database tables

    • Terminal command :python manage.py migrate

  1. after , Let's run the project , Input http://127.0.0.1:8000/admin Take a look at the effect

3、 ... and 、 Database background construction

  1. By the time the landing page is almost half finished , Next, you need to create an administrator account to log in ,

    • Terminal command :python manage.py createsuperuser

  1. After creating the account, you can log in , The effect is as follows

3. Because it's all in English , You can't stand the code , I want to change it into Chinese settings.py Lieutenant general LANGUAGE_CODE Medium 'en-us' Change to 'zh-Hans', Does this look much more comfortable

Four 、 Create a database model

  1. stay sites Medium models Create a model of a database table , Here's the picture , Field has title、url、body、create_date

5、 ... and 、 Activate the model
This step is crucial , Every time you modify model The data in it , The following steps should be performed from the beginning :

  1. stay myBolg/settings.py in , modify INSTALLED_APPS, Import the Application, We have only one sites, It has been quoted , So there is no need to quote

  2. Execute terminal commands :

    • python manage.py makemigrations

    • python manage.py migrate

  1. take model Add to sites/admin.py in

  2. Run the project , Open the management interface , See the effect , Then you can add it at will Article Content http://127.0.0.1:8000/admin


     

6、 ... and 、 Pit summary

  1. UnicodeEncodeError: 'ascii' codec can't encode characters  
    Previous model model There's a way in def str(self): This is the method used in the old version ,Django The new version should be changed to def unicode(self):, This solves the problem of string passing errors , The unified code is Unicode character string .

  2. OperationalError no such table / OperationalError  table sites_article has no column named test
    This is a kind of mistake , That's what I reminded you , We add model Or modify models.py After the code inside , Step 5 needs to be performed again , The function of step 5 is to update the information of the database table , For example, you add model A field in , Database table not updated , Naturally, the above mistakes will be reported
    resolvent : Run the terminal command in step 5

    • python manage.py makemigrations

    • python manage.py migrate

Information 、 Source code 、 The courseware solution can be obtained by scanning the code below ~


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved