How to install Django in a virtual environment under Ubuntu

How to install Django in a virtual environment under Ubuntu

Perform the following operations in the Ubuntu command line window:

1. Install the virtual environment

sudo pip install virtualenv

2. Create a virtual environment

mkvirtualenv 文件名-p python3(這是python版本)

Some friends have questions about the "mkvirtualenv file name", which defaults to the python2 version

However, please note that the operation here needs to be done online. Copy a copy of Python to the virtual environment

3. Switch virtual environment

workon 文件名

Work in a virtual environment

Note: Deleting the virtual environment

rmvirtualenv filename

Exit environment

deactivate

Installation of Django in a virtual environment and establishment of the project 1. Install Django

pip install django ==安裝版本號

2. Create a project

django_admin startproject + 項目名字

The project contains two files (one is a folder with the same name as the project, and the other is a manage.py file): manage.py is the overall management file of the project, through which the entire project is managed. The folder with the same name as the project: settings.py is the overall configuration folder of the project urls.py is the project URL configuration file wsgi.py is the entry point of the project and the web server

3. Run the server

python manage.py runserver +端口名(默認為8000)

After the operation is successful, you can confirm whether it is successful by going to the browser (127.0.0.1: port name)

4. Create Modules

After successfully creating a project, you can use it under the project:

python manage.py startapp +模塊名

To create a module

Subfiles of the module named user

5. Register submodule

In the setting.py file under the folder of the same name

Add "module name.apps.class name" to the last line

6. Define table name and field name

Class name——table name (actually the table name is module name_class name) attribute name——field name

7. Register the designed table in admin.py

8. Migrate database tables (steps 8 and 9 must be performed every time the database table is modified)

python manage.py makemigrations

Generate migration files (in the file migrations)

9. Execute the migration file

python manage.py migrate

Execute the migration file

10. Backstage display

After executing the migration file, you can create a super administrator account to manage and view the table in the background.

python manage.py createsuperuser

If you want Django to support Chinese interface, you need to modify it in the setting.py file:

LANGUAGE-CODE = 'zh-Hans' //Set Chinese TIME_ZONE='Asia/Shanghai' //Set time zone (Shanghai)

11. Database Configuration

If you want to modify the content in the database on the front end, we need to configure the database

The above setting is configured in the setting.py file

12. Add driver

After configuring the database, we need to add the driver to the __init__.py file in the folder of the same name in the project, as shown in the figure:

Summarize

The above is the operation method of installing Django in the virtual environment under Ubuntu introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website! If you find this article helpful, please feel free to reprint it and please indicate the source. Thank you!

You may also be interested in:
  • Analysis of the process of installing mongodb on ubuntu, creating accounts and libraries, and adding coordinate indexes
  • How to configure the Qt development environment of the Go language on Ubuntu Linux
  • Detailed summary of deploying Django+uwsgi+nginx in Ubuntu environment
  • Detailed tutorial on building django+nginx+uwsgi on Ubuntu
  • Detailed tutorial on installing MongoDB 4.0 on Ubuntu 18.04
  • Detailed tutorial on manually installing MongoDB on Ubuntu 16.04
  • Detailed process of installing mongodb 3.4 under Ubuntu
  • Analysis of the process of building a GO language development environment on Ubuntu 18.04 LTS

<<:  Vue must learn knowledge points: the use of forEach()

>>:  MYSQL custom function to determine whether it is a positive integer example code

Recommend

Docker-compose tutorial installation and quick start

Table of contents 1. Introduction to Compose 2. C...

Steps to use ORM to add data in MySQL

【Foreword】 If you want to use ORM to operate data...

Detailed graphic explanation of how to use svg in vue3+vite project

Today, in the practice of vue3+vite project, when...

Introduction to the use of MySQL official performance testing tool mysqlslap

Table of contents Introduction Instructions Actua...

Count the list tags in HTML

1. <dl> defines a list, <dt> defines ...

CSS Reset style reset implementation example

Introduction: All browsers come with default styl...

A brief discussion on the $notify points of element

My original intention was to encapsulate the $not...

Installation process of zabbix-agent on Kylin V10

1. Download the installation package Download add...

What are the rules for context in JavaScript functions?

Table of contents 1. Rule 1: Object.Method() 1.1 ...

Detailed explanation of redo log and undo log in MySQL

The most important logs in the MySQL log system a...

MySQL master-slave replication principle and points to note

Written in front I have been writing a special to...

JavaScript Basics: Immediate Execution Function

Table of contents Immediately execute function fo...

Solution to MySql service disappearance for unknown reasons

Solution to MySql service disappearance for unkno...

JavaScript to implement retractable secondary menu

The specific code for implementing the retractabl...

Vue component to realize carousel animation

This article example shares the specific code of ...