Get the App
SLTechnology News&Howtos  ›  Database  › 

Django from entry to abandonment. Preliminary study

Shulou Source: shulou.com Published: 2022-06-01 19:33:55 10月02日 Update

Environment: in order to enable the server to develop multiple applications, the environment version does not affect each other, using virtualenv to create an independent and isolated environment for development and use.

# install virtualenv:

[root@vps django] # pip3 install virtualenv

# create a virtual environment

[root@vps django] # virtualenv django_env

Using base prefix'/ usr/local'

New python executable in/ django/django_env/bin/python3.5

Also creating executable in/ django/django_env/bin/python

Installing setuptools, pip, wheel...done.

# using a virtual environment

[root@vps django] # source django_env/bin/activate

# install django in a virtual environment

(django_env) [root@vps django] # pip3 install django==1.10

Terminology:

The role of project is to provide configuration files, such as where to define database connection information, list of installed app, TEMPLATE_DIRS, and so on.

An app is a collection of Django functions, usually including models and views, that exist as a package structure of the Python.

For example, Django itself has some app built into it, such as an annotation system and an automatic management interface. One of the key points of app is that they are easily portable to other project and reused by multiple project.

# 1. Create a project project

(django_env) [root@vps django] # django-admin startproject my_project

After you create a project, several files are generated

# 2. You can create an application app in either of the following two ways

(django_env) [root@vps my_project] # django-admin startapp first_app

(django_env) [root@vps my_project] # python3 manage.py startapp second_app

# 3. Connect to the database and use pymsql to connect to MySQLdb in Python3, otherwise you will encounter the following pit

Solution:

1)。 Install pymysql wheels:

(django_env) [root@vps my_project] # pip3 install pymysql

2)。 After the execution is successful, open _ _ init__.py and add the following:

3)。 Modify settings.py:

4)。 Synchronize the database:

This method can create tables, and when you add a class to models.py, run it to automatically create tables in the database instead of manually.

(django_env) [root@vps my_project] # python3 manage.py migrate

After the database synchronization is successful, some tables are generated in the connected library.

# 4. Use your own development server, which can only be debugged during development. Online use is not recommended.

(django_env) [root@vps my_project] # python3 manage.py runserver 0.0.0.0 purl 8000

Performing system checks...

System check identified no issues (0 silenced).

January 04, 2017-02:24:16

Django version 1.10, using settings' my_project.settings'

Starting development server at http://0.0.0.0:8000/

Quit the server with CONTROL-C.

[04/Jan/2017 02:25:15] "GET / HTTP/1.1" 200 1767

Not Found: / favicon.ico

# create a login account password at the backend

(django_env) [root@vps my_project] # python3 manage.py createsuperuser

Username (leave blank to use 'root'): admin

Email address: admin@admin.com

Password:

Password (again):

The true face of Lushan Mountain

Tags: Environment data database development method success multiple file server project synchronization application service generation Lushan true face no function key key point function Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Docker Shulou Technology Apple MariaDB