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

Example of using mycat to implement MySQL database read-write separation

What is MyCAT A completely open source large data...

MySQL database table and database partitioning strategy

First, let's talk about why we need to divide...

Use the njs module to introduce js scripts in nginx configuration

Table of contents Preface 1. Install NJS module M...

Example of removing json backslash in php

1. Remove backslashes through the "stripslas...

Native JS implementation of loading progress bar

This article shares a dynamic loading progress ba...

When is it appropriate to use dl, dt, and dd?

dl:Definition list Definition List dt:Definition t...

Basic HTML directory problem (difference between relative path and absolute path)

Relative path - a directory path established based...

A brief analysis of CSS :is() and :where() coming to browsers soon

Preview versions of Safari (Technology Preview 10...

Sending emails in html is easy with Mailto

Recently, I added a click-to-send email function t...

WEB standard web page structure

Whether it is the background image or the text siz...

Summary of common MySQL table design errors

Table of contents Mistake 1: Too many columns of ...

MySQL 5.7 zip archive version installation tutorial

This article shares the installation tutorial of ...

Detailed explanation of HTML basic tags and structures

1. HTML Overview 1.HTML: Hypertext Markup Languag...