Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Django

2025-01-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how to use Django. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

1. Create a Django project and directory structure

Creating a Django project is as simple as typing on the command line after installing Django:

Django-admin startproject project name example: create project BookManagerdjango-admin startproject BookManager

Will be able to complete the creation of one of our projects.

Of course, if you are using a pycharm editor, you can also create a Django project directly

After the basic project has been created, let's take a look at what files are in the project directory after creation:

BookManager: a file with the same name as the project, which contains the following configuration files.

Init.py: indicates that the file BookManager can be used as a package.

Settings.py: is the overall configuration file of the project.

Urls.py: is the URL configuration file for the project.

Wsgi.py: is the WSGI compatible Web server entry for the project.

Manage.py: is the entry point for the project to run, specifying the configuration file path.

two。 Create and register applications

In the previous article, we talked about the software framework, in which there is a saying: "A software framework is made up of multiple software modules, and each software module has its own function." the combination of each function completes the development of a software. "

The application here refers to the application that completes a function. In Django, a project consists of many applications, each of which corresponds to a function.

How to create an application?

Now in the BookManager project, create an application Book

Cd BookManager/ python manage.py startapp Book

In the figure, I show you the difference between the application and the project itself, so what is the specific role of each file in the application folder?

Admin.py: is the background site management registration file.

Init.py: indicates that the file Book can be used as a package.

Migrations: model migration is done.

Models.py: M in .MVT that processes data

Tests.py: it's for testing.

Views.py: the V in .MVT that deals with business logic

The command we executed above only created the application, but in fact our project does not recognize it, we need to register the application in the setting.py file.

3. Test server

After you know how to create projects and applications, we need to run our project to see if it works properly.

In the development phase, in order to quickly preview the effect of development, Django provides a lightweight web server written in pure Python (used only in the development phase, informal server).

Run the command for the test server:

Python manage.py runserver ip: Port python manage.py runserver 172.16.21.25 8001

Corresponding: http://172.16.21.25:8001/

Ip and port number can be left unwritten. The default ip is 127.0.0.1; the port number is 8000.

Python manage.py runserver

Corresponding: http://127.0.0.1:8000/

Enter http://127.0.0.1:8000/ to access it.

Refresh the page to see the effect of each code execution.

If you add code, modify code, delete code, the server will restart automatically.

Press ctrl+c to stop the server

If you can see that the following page appears, then Django is running successfully.

Thank you for reading! This is the end of the article on "how to use Django". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 214

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report