preface

In this tutorial, you will learn how to use the Django framework to develop hello World applications. This tutorial is intended for beginners in Python and Web development, but requires basic knowledge of Python syntax. I mainly use python3 in windows10 system development.

Django is introduced

Django is a Python web development framework for writing Web applications. We know that web applications use THE HTTP protocol for transmission, which is essentially a string of text that is sent back and forth between the client and the server, with requests and responses, so it would be very difficult to write a Web application from scratch, and we would need to parse the content for the requests sent by the client. According to the corresponding content to make different responses, and finally send the user to him (HTML document), so you can use some predecessors to write a good framework for development, so only need to write their own procedures of logical functions, and do not need to pay attention to a lot of low-level work.

Install Django

Django is a third-party Python library, so you need to install it before you can use it. Here, use the PIP command to install django2.2, the long-supported version.

PIP install django = = 2.2Copy the code

Create a project

Once installed, we are ready to create our Web application project, called HelloDjango. Open our Windows command line, CMD or Powershell, and run the following command.

cd  C:\Users\Derek\Desktop\
django-admin startproject HelloDjango
Copy the code

Note that in the first command Derek needs to be replaced with the username you logged in to your computer, meaning to switch the working directory to the desktop. The second line, installing Django will also install a command line program called Django-admin that will be used to create our project. A folder called HelloDjango will appear on the desktop. This is our project folder. Double-click to open it and you’ll see a HelloDjango folder and a script called Manage.py.

The manage.py script is a utility that will be used later. The HelloDjango folder represents our project itself, and you’ll see a lot of files when you open it.

  • Init.py: an empty file that tells Python that this folder is a Python package.
  • Wsgi.py: An entry point for wsGi-compliant Web servers to serve projects. Usually leave the file as it is and leave it alone.
  • Settings. py: Contains Django project Settings that can be changed during Web application development.
  • Urls.py: Contains the route to the web page, matching the entered URL address to the specific function.

Start the server to test if the site works properly

We need to start the service at the same level as the manage.py file. Since our working directory was on the desktop, we now need to switch inside the project folder HelloDjango and run the following command.

cd .\HelloDjango\
python .\manage.py runserver
Copy the code

At this point our server has been started and normally outputs the following text.

Starting Development Server at http://127.0.0.1:8000/ 127 This address is the local address of our computer that only we can access. You can now copy the url to your browser and open it. If you see the following interface that everything is OK!

Create an

You can now temporarily close the browser window and follow the instructions in the terminal output window by pressing Ctrl+C to stop the server. A Django project can contain multiple application modules, much like a supermarket divided into fruit sections, drinks sections, and snacks sections. So now we need to create our first application for our project and call it Main, the main application.

python manage.py startapp main
Copy the code

We now have a main folder in our project file, which still contains many files, similar to helloDjango, such as views.py (which contains functions used to define pages in the Web application) and models.py (which contains classes used to define data objects).

Create function functions

Now we need to create what we call a functional function, which means that the function is used to respond to the user. In other words, it returns some text or HTML document content to the end user’s browser. In our case, it outputs a Hello Django message to the browser interface. This involves editing the main/views.py file. I’ll use VS Code to open up the project folder HelloDjango to show you the current file structure again.

Now, edit the main/views.py file and type in the following code.

from django.http import HttpResponse def home(request): return HttpResponse("Hello, Django!" )Copy the code

Here we import an HttpResponse class that generates an HTTP response object. It generates a string, and then creates a home function that represents the home page of the web site. The HTTP response object of the. But at this point, our site can’t return the HelloDjango words back to the user’s browser. We still need to bind something called a route to this function.

Bind URL Routing

First, create a file called urls.py in the main folder and type the following code.

from django.urls import path
from . import views

urlpatterns = [
    path("", views.home, name="home"),
]
Copy the code

Second, there is also a urls.py file in the helloDjango folder, the project folder. We edit it and type the following code.

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

urlpatterns = [
    path('admin/', admin.site.urls),
    path("", include("main.urls")),
]
Copy the code

To explain, there are two urls.py files that determine the route, one under the main item and one under the app. After the user types http://127.0.0.1:8000/ in the browser, First go to the urls.py of the overall route to the project. This is where the code include(“main.urls”) kicks in and you see that there is no deeper path to the home page, the url, Such as http://127.0.0.1:8000/home, or http://127.0.0.1:8000/derek/home/user directly to transfer request to “main. Urls”, that is just the inside of the application urls. Py files, The path(“”, views.home, name=”home”) will give you the home function from the view file if you visit the home root directory, so we start the server again. You’ll see the previously defined “Hello, Django!” The words, accomplished!

python .\manage.py runserver
Copy the code