Article From:https://www.cnblogs.com/huahua2018/p/9970205.html
DjangoStudyPIP install virtualenv1. Installing Virtual EnvironmentCMDH:CD dir envsVirtualenv mytest virtualCD mytestvirtual/ScrIpts/activate Start Virtual EnvironmentperhapsPIP install virtual wrapperAdd envs to system environment variablesCD envsWorkon mytestvirtUAL Starts Virtual Environment2. Run the virtual environment and install Django* In the Django project, the organizational structure of the project is as follows: a project contains multiple applications, and an application corresponds to a business module.* Example: Create a project named BookMangerCreate an application named Book* Create a project in a directory of the current user so that no permission issues occur3. Create a Django projectCreate projects on the desktop: CD ~/Desktop/Command to create a project: django-adminStart project BookMangerCD ~/Desktop/BookManger/Tree /fBookManger folder with the same nameDocument: manage.py4. Creating Django applicationsPython management.py startapp BookGenerate Book Application Folder5. pycharm opens the BookManger projectWhere Python looks at python. exe of the virtual environmentAbsolute pathTurn the python environment of this column into a virtual environment6. Configure settings.py to increase our Book applicationINSTALLED_APPS=['django. contrib. admin','django. contrib. auth','django. contrib. contenttypes','django. contrib. sessions','dJango. contrib. messages','django. contrib. static files',# Installation Application'Book',]7, operationPython manAge.py runserver Python test serverHttp://127.0.0.1:8000/Browser accessibleCreate success8. Using Django to Develop Database*****************************************************************************************************************************************************Model in MVT Design Patterns, which is specifically responsible for interacting with databasesModels.pyBecause the ORM framework is embedded in Model, there is no need for direct database-oriented programming.Instead, it defines model classes and completes the addition, deletion and alteration of data tables through model classes and objects.ORM framework is to establish rows and corresponding objects of data tableRelevance and mutual transformation make the operation of database object-oriented**************************************************************************************************** * * * * ** Define model classesClassBookInfo (models. Model):"Defining Model Classes of Book Information"Name designName = models.CHarField (max_length = 10)Class PeopleInfo (models. Model):"Model Class for Defining Personal Information"Name = models.CHarField (max_length = 10)Gender = models. BooleanField ()Book = models. ForeignKey (BookInfo) # foreign keySet up# models. ForeignKey (BookInfo, on_delete = models. CASCADE)* Model migrationGenerate migration:Python management.py makemigrAtions*****************************************************************************************************************Book migrations 1_initial.py- Create model BookInfo- Create model PeopleInfo*************************************************************************************************************************Execute migration:***********python ManaGe.py migrate***********Generate db.sqlite3===================================================================== MYSQL==============================================================Https://www.lfd.uci.edu/~gohlke/pythonlibs/#mysqlclientDownload WHLPIP install ***. WHLCREATE DATABASE database_name CHARACTER SET UTF8; specified database code utf8DATABASES = {# default SQLite3#'default': {#'ENGINE':'django. db. backends.'default': {#'ENGINE':'djAngo. db. backends. sqlite3',#'NAME': os. path. join (BASE_DIR,'db. sqlite3'),"#'NAME': os. path. join (BASE_DIR,'db. sqlite3'),I am sorry.# Configure MySQL'default': {'ENGINE':'django.db.backends.mysql','NAME':'bookdjango', # database name'HOST':'loca'Lhost','PORT': 3306,'USER':'root','PASSWORD':'root',}}Enter PythonAnage.py sqlmigrate appName 0001 view generated SQL syntaxIf SQL is correct, you cannot create a new data table:Error raise Migration SchemaMissing ("Unable"To create the django_migrations table (% s) "% exc)1. The permission to modify the database failed in Version 5.55.2. Install the version of Django into 2.0 pipsTall Django==2.0.0-i https://pypi.douban.com/simple***********python management.py sqlmigrate Book 0001** * * * * *Book app_name application name 0001 migration_name file 0001_initial.py====================================================MYSQL=========================================================* Operating databasesBackground web access to Django sitesSite: It is divided into two parts: content publishing and public access.Content Publishing: WebsiteAdministrators copy, view, add, modify, delete dataUsing django's management module, the following steps are required:* Localization of management interfaceLanguage and Time SettingsLANGUAGE_CODE='en-us'TIME_ZONE='UTC'= = =LANGUAGE_CODE='zh-Hans'TIME_ZONE='Asia/Shanghai'* Create an AdministratorPython management.py create superuserAdmin 2018 HuahuaPython management.py runserver* Register model classesAdmin.pyRegister Book InformationAdmin. site. register (BookInfo))Registered Person InformationAdmin. site. register (PeopleInfo)* Publish content to database* Customize site management pagesModels.pyChange partClass BookInfo (m)Models.Model:"Defining Model Classes of Book Information"Name designName = models. CharField (max_length = 10)Def_u str_ (self):""""Output model in the form of strings""""Return self.nameChange a site page of a certain typeModels.pyClass PeopLeInfoAdmin (admin. ModelAdmin):"Site Management Class Web of Personal Information Model Class", "Web of Personal Information Model Class", "Web of Personal Information Model Class" and "Web of Personal Information Model Class"List_display=['id','name','gender','book']Register Book InformationAdmin. site. register (BookInfo)Registered Person InformationAdmin. site. register (PeopleInfo, PeopleInfo)OAdmin) # Registration Person Information Class and Person Site Information Management Class

  

Leave a Reply

Your email address will not be published. Required fields are marked *