Django project getting started example

This article: Install Django, create a Django project, create an app, start and run the Django project, and make a small entry-level demonstration for novices.

Table of contents

1. Preparation:

1. Create a virtual environment

2. Enter the virtual environment

2. Use of Django

1. Install Django

2. Create a Django project

3. Create APP

4. Register App

5. Write urls.py under the PacketGUI file:

6. Write views.py in the show folder:

7. Start the django project and run the web service

8. Write html files in the templates directory


1. Preparation:

1. Create a virtual environment

1) Open the command line interface of Anaconda and create a virtual environment in Anaconda (here I named the virtual environment: packetgui), specifically used to install and run the Django project to avoid collisions with other codes.

conda create -n packetgui python=3.6   #(创建一个名字为packetgui的虚拟环境)

2) After creating the virtual environment packetgui, you can see the packetgui folder in the envs folder under the anaconda installation directory.

2. Enter the virtual environment

Enter the virtual environment packetgui, and all subsequent operations will be performed in this virtual environment.

conda activate packetgui    #进入虚拟环境packetgui

2. Use of Django

1. Install Django

1) In the created packetgui virtual environment, pip install django:

pip install django  #安装django

2) After successful installation, you can see the source code of the django framework in the lib——>site-packages folder. You can see django-admin.exe in the Scripts folder.

 

2. Create a Django project

1) Enter a directory:

        cd directory name #Here I choose to place the project in the F:/Work directory

2) Execute the command to create the project:

django-admin startproject PacketGUI   #创建一个名字叫作PacketGUI的项目

 

 3) After creating the project, you can see it in the file directory:

The created project is as shown in the figure below. A PacketGUI folder will appear under the F:\Work folder.

 

3. Create APP

1) Enter the project directory: cd PacketGUI

2) Execute the command on the command line to create an app named show:

​python manage.py startapp show   #创建一个名字为show的app

4. Register App

Register the just-created app named show in setting.py:

5. Write urls.py under the PacketGUI file:

from django.contrib import admin
from django.urls import path

from show.views import index, show_packet  # 从创建的'show'这个app的views.py里导入定义的index函数

urlpatterns = [
    # path('admin/', admin.site.urls),
    path('index/', index, name='index'),  # 将url('index/') 和函数(views.py里定义的index) 对应起来
]

6. Write views.py in the show folder:

from django.shortcuts import render, HttpResponse

# Create your views here.
def index(request):  # 定义index函数
    return HttpResponse("欢迎使用")

 That is, the contents in urls.py and views.py must correspond one-to-one: 

7. Start the django project and run the web service

1) python manage.py runserver #Start the django project

 2) Then enter: 127.0.0.1:8000/index in the browser to access the index page.

8. Write html files in the templates directory

Create a new templates directory in the app directory, and create a new show_packet.html file in the templates directory, urls.py and view.py must also correspond.

1) urls.py under PacketGUI file: 

from django.contrib import admin
from django.urls import path

from show.views import index, show_packet # 从创建的'show'这个app的views.py里导入定义的index函数

urlpatterns = [
    # path('admin/', admin.site.urls),

    # 将url('index/') 和函数(views.py里定义的index) 对应起来
    path('index/', index, name='index'),  

    #将url:show_packet/ 和函数show_packet对应起来
    path('show_packet/', show_packet, name='show_packet'), 
]

2) views.py in the show folder:

from django.shortcuts import render, HttpResponse

# Create your views here.
def index(request):  # 定义index函数
    #return HttpResponse("欢迎使用")
    return render(request, 'index.html')  #转向index.html

def show_packet(request):  # 定义show_packet函数
    # 1.去app目录下的templates目录寻找show_packet.html
    return render(request, 'show_packet.html')  #转向show_packet.html

 3)show_packet.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>显示流量页面</h1>
</body>
</html>

4) Enter: 127.0.0.1:8000/show_packet in the browser to access the show_packet page.

 

 

reference:

2022 Station B’s most detailed django3 tutorial (django from entry to practice)_bilibili_bilibili

https://blog.csdn.net/wojiucaonen/article/details/97787764

https://blog.csdn.net/css_aaa/article/details/90638983

Guess you like

Origin blog.csdn.net/weixin_39450145/article/details/125173992