Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize simple HelloWord Page with Python+Django

Shulou Source: shulou.com Published: 2022-05-31 20:07:12 09月17日 Update

This article mainly introduces Python+Django how to achieve a simple HelloWord page related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this Python+Django how to achieve a simple HelloWord page article will have a harvest, let's take a look at it.

Install Django

Use anaconda to install the Django package in the python environment

Pip install django creates a Django project

Create on the command line using django-admin

Django-admin startproject myproject

Professional version of PyCharm creates django project

Default file

After the project is created, a directory with the same name as the project is generated, as well as a manage.py file

1. Manage.py project management, start the project, create app, data management, this file does not need to be changed

2. In myproject, what we need to write and change are settings.py and urls.py

Urls.py-URL and function correspondence [frequently operated files] settings.py---- project configuration files, such as database connection configuration, static file configuration, APP application configuration, etc., create APP

Large-scale projects generally have many functional divisions. Django will encapsulate these functions in app, that is, without application. Each app has independent functions, static files, etc., and each app does not affect each other.

Python manage.py startapp app

With the above command, you can create an application called app.

Migrations- database changes admin.py- django provides admin background management apps.pymodels.py- for database operations [!! Tests.py- is used to do unit tests. Views.py- defines view functions [! ]

Generally, after creating APP, we need to write views.py function and models.py function, which correspond to URL view function and database operation respectively.

Implement a simple HelloWord web page

First, make sure app is registered.

In the settings.py configuration file, add the app application you created

Write the correspondence between URL and view function

Write correspondence in myproject and view function in views.py of app

App / views.py

From django.shortcuts import renderdef test (request): return render (request,'test.html')

Mypeoject / urls.py

From django.urls import pathfrom app import viewsurlpatterns = [path ('app/test',views.test)]

That is, the test view function corresponding to the app / test route will return a test.html

Start the django project python manage.py runserver 8000

Enter 127.0.0.1:8000/app/test on the web page

This is the end of the article on "how to implement a simple HelloWord web page with Python+Django". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to achieve simple HelloWord web pages with Python+Django". If you want to learn more, you are welcome to follow the industry information channel.

Tags: Functions projects files web pages data views configuration databases applications knowledge management content functions commands articles static no professional inter-value Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Xiaomi Huawei Apple Microsoft