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

What is the method of creating a project in python's Django framework?

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "what is the method of creating a project in python's Django framework". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the method of creating a project in python's Django framework".

The details are as follows:

Django management tools

After you install Django, you have an available administrative tool, django-admin.py. We can use django-admin.py to create a project:

We can take a look at the command introduction of django-admin.py:

Type 'django-admin.py help' for help on a specific subcommand.

Available subcommands:

[django]

Check

Compilemessages

Createcachetable

Dbshell

Diffsettings

Dumpdata

Flush

Inspectdb

Loaddata

Makemessages

Makemigrations

Migrate

Runserver

Sendtestemail

Shell

Showmigrations

Sqlflush

Sqlmigrate

Sqlsequencereset

Squashmigrations

Startapp

Startproject

Test

Testserver

Create a project

Use django-admin.py to create a djangoPro project

Django-admin.py startproject djangoPro

For the latest version of Django, use the django-admin command:

Django-admin startproject djangoPro

The directory structure of the project after creation:

$cd djangoPro/

$tree

.

|-- HelloWorld

| |-- _ _ init__.py |

| |-- settings.py |

| |-- urls.py |

| | `--wsgi.py |

`--manage.py

Catalogue description: which is a good https://yiyuan.120ask.com/art/ in Zhengzhou Gynecology Hospital

DjangoPro: the container for the project.

Manage.py: a practical command line tool that allows you to interact with the Django project in a variety of ways.

DjangoPro/init.py: an empty file that tells Python that the directory is a Python package.

DjangoPro/settings.py: the setup / configuration of the Django project.

DjangoPro/urls.py: the URL statement of the Django project; a website "directory" driven by Django.

DjangoPro/wsgi.py: a portal to a WSGI-compatible Web server to run your project.

Next, let's enter the djangoPro directory and enter the following command to start the server:

Python3 manage.py runserver 0.0.0.0:8000

0.0.0.0 allows other computers to connect to the development server, with 8000 as the port number. If not, the port number defaults to 8000.

Enter the ip and port number of your server in the browser. If you start normally, the output is as follows:

View and URL configuration

Create a new view.py file in the djangoPro directory under the previously created djangoPro directory, and enter the code:

From django.http import HttpResponse

Def hello (request):

Return HttpResponse ("Hello world!")

Next, bind the URL to the view function. Open the urls.py file, delete the original code, and copy and paste the following code into the urls.py file:

From django.conf.urls import url

From. Import view

Urlpatterns = [

Url (r'^ $', view.hello)

]

You can also modify the following rules:

From django.conf.urls import url

From. Import view

Urlpatterns = [

Url (r'^ hello$', view.hello)

]

Note: if the code changes in the project, the server will automatically monitor the code changes and reload automatically, so if you have started the server, you do not need to restart manually.

Url () function

Django url () can accept four parameters, two required parameters: regex, view, and two optional parameters: kwargs, name, which are described in more detail next.

Regex: regular expression, and the matching URL executes the corresponding second parameter, view.

View: used to execute URL requests that match regular expressions.

Kwargs: the parameter of the dictionary type used by the view.

Name: used to get URL in reverse.

Thank you for your reading, the above is the content of "what is the method of creating a project in python's Django framework". After the study of this article, I believe you have a deeper understanding of what the method of creating a project in python's Django framework is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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: 0

*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

Development