First Django project: HelloWorld

OS: Windows Home Chinese, Python: 3.6.3, Django: 2.0.3

 

In the previous article, Django has been successfully installed into Python. Now, let's develop your first Python project!

1. A preliminary study of the django-admin command

After installing Django, django-admin is ready to use. This command is located under C:\Python36\Scripts, django-admin.exe, a Windows executable.

Enter django-admin on the command line to view its related information. There are many subcommands under it. This article uses the startproject command:

More content of the django-admin command needs to be studied.

 

2. Create your first project using django-admin

Open the command line and enter the directory where the project was created (it can also be specified in the command to create the project) ;

Execute the Django project creation command: django-admin.py startproject HelloWorld ;

There is no prompt, the HelloWorld project was created successfully!

After the creation is successful, the project folder HelloWorld of the HelloWorld project will appear in the current directory;

That's it, the project is built!

Simple, right? The first step is of course easy!

So, what is the meaning of each file? Please see article 1 of the reference link, which says very clearly:

 

3. Start the project server

Question: Does this server belong to the HelloWorld project alone, or does it belong to the entire Django? Can multiple projects be run at the same time?

As introduced in the previous section, manage.py in the project directory is a command-line tool. Here, we need to use its subcommands to start the HelloWorld server.

Enter at the command line: python3 manage.py runserver

The reasons for using Python3 will not be further stated.

After the server starts:

The problem of "You have 14 unapplied migrations(s)...." indicated in the above figure has not been resolved and needs to be further studied.

The penultimate sentence in the picture above prompts: Starting development server at http://127.0.0.1:8000/, which means that the server (to be precise, [development server], is there any other server?) has been

Start, you can use http://127.0.0.1:8000/ to access.

Enter http://127.0.0.1:8000/ in the browser, press Enter, you can see the following result: A default Web view (interface) is opened! It means that the project server has been started successfully!

 

Command line output after visiting the page:

As you can see, some files starting with /statc are sent to the page. Where do these files come from?

When checking the static in the settings.py file, I found the following variable: django.contrib.staticfiles

Keep looking, and find that the above file does come from the Django installation directory:

 

Continue to research and find that the page is generated according to view.py in the project directory, and the URL rules are determined by the urls.py file!

 

It should be noted that the default port for this development server is 8000, which can be changed on the startup command line.

The usage details of this command line tool can be viewed with the command python3 manage.py.

To view the details of the runserver command, you can use python3 manage.py help runserver to view:

 

4. Let the page output Hello world!

Delete view.py and urls.py in the project, and re-create the two files, the contents are as follows:

#view.py
from django.http import HttpResponse
def hello(request):
    return HttpResponse("Hello world!")

#urls.py
from django.conf.urls import url
from . import view
urlpatterns = [
    url('^$', view.hello)
]

 

Restart the project server, and then visit the page to get the Hello world! we need:

The command line output at this point is:

 

Reference link:

Django creates the first project

 

Follow-up research: How to create view.py, url.py, etc. The rules of the Django project are just getting started.

 

====END====

Finally, we need to figure out what exactly is Django? Is Django a web server? Why should Django be used with Nginx and Apache?

Django is an open source web application framework written in Python. The frame pattern of MTV is adopted, namely model M, template T and view V. (Baidu Encyclopedia)

Django is a framework constructed based on MVC. (Baidu Encyclopedia)

The main purpose of Django is to easily and quickly develop database-driven websites. (Baidu Encyclopedia)

Django can run on Apache, Nginx, or on servers that support WSGI and FastCGI. (Baidu Encyclopedia)

The answer given in the following article is very good, thank you netizens:

Why does django use uwsgi and nginx when deploying online as a web server?

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325200360&siteId=291194637