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

Design Theory: Text Legibility and Readability

<br />Not long ago, due to business needs, I...

js to achieve simple accordion effect

This article shares the specific code of js to ac...

Detailed explanation of JavaScript animation function encapsulation

Table of contents 1. Principle of animation funct...

Vue echarts realizes dynamic display of bar chart

This article shares the specific code of vue echa...

CSS float property diagram float property details

Using the CSS float property correctly can become...

Detailed analysis of the parameter file my.cnf of MySQL in Ubuntu

Preface Based on my understanding of MySQL, I thi...

100-1% of the content on the website is navigation

Website, (100-1)% of the content is navigation 1....

How to implement Mysql switching data storage directory

How to implement Mysql switching data storage dir...

JavaScript Canvas draws dynamic wireframe effect

This article shares the specific code of JavaScri...

HTML optimization speeds up web pages

Obvious HTML, hidden "public script" Th...

Detailed discussion of memory and variable storage in JS

Table of contents Preface JS Magic Number Storing...

HTML Editing Basics (A Must-Read for Newbies)

Open DREAMWEAVER and create a new HTML. . Propert...

How to build a React project with Vite

Table of contents Preface Create a Vite project R...