** The concept of a view in Django is “a collection of web pages with the same functionality and template.” ** For example, in a blog application, you might create the following views:

  • Blog front page: Shows the most recent items.
  • Content Details page: displays a content in detail.
  • Comment handler: Used in response to adding comments to an item of content.

These requirements are accomplished by the View.

Hello World!

Start by writing the simplest view function that prints Hello World! In the browser. A string.

Open article/views.py and write the view function:

article/views.py

Import the HttpResponse module
from django.http import HttpResponse

View function
def article_list(request):
    return HttpResponse("Hello World!")
Copy the code

** In Django, web pages are derived from views. ** Each view is represented as a simple Python function that must do only two things: return an HttpResponse object containing the content of the requested page, or throw an exception, such as Http404. As for what else you want to do, be my guest.

The request in the view function is related to the request sent by the web page, which contains the content of GET or POST, user browser, system and other information. When Django calls the article_list function, it returns an HttpResponse object containing a string.

** With view functions, you also need to configure URLconfs to associate URL links requested by the user. ** In other words, URLconfs maps urls to views.

The URL of the project /article was already distributed to the Article app in the previous article, so you just need to modify the article/urls.py that was added earlier. Add the following:

article/urls.py

# introducing views. Py
from . import views

...

urlpatterns = [
    The # path function maps the URL to the view
    path('article-list/', views.article_list, name='article_list'),]Copy the code

**Django will choose which view to use based on the URL requested by the user. ** In this example, when the user requests an article/article-list link, the article_list function in views.py is called and the rendered object is returned. The name parameter is used to reverse lookup the URL, giving the URL a name that will be used later.

Test that the code you just typed works.

In a virtual environment, go to the project directory, the my_blog folder, and type Python manage.py runserver to run the debug server:

(env) E:\django_project\my_blog>python manage.py runserver Performing system checks... System Check identified no issues (0 silenced). August 30, 2018-19:41:00 Django Version 2.1, using Settings'my_blog.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Copy the code

Successfully running, open a browser and enter the url address, http://127.0.0.1:8000/article/article-list/, including 127.0.0.1:8000 is the local address of debug server, Article is the address that the project route my_blog\urls.py distributes, and article-list is the address that the article\urls.py application just configured distributes.

With any luck, the browser will print Hello World! String:

Isn’t it amazing that the basic functionality is done in less than 10 lines of code?

Of course, it’s just a test run.

The preparatory work

Although the database table was defined in the previous section to write the Model Model, the table was empty, which was not convenient to show the effect of View fetching data. So before we write the View, we need to record some data in the table. So let’s do this.

Website background concept

Website background, sometimes called website management background, refers to a series of operations used to manage the website, such as: data increase, update, delete, etc. In the early stage of project development, because there is no real user data and complete test environment, the background will be frequently used to modify test data.

Fortunately, Django comes with a nice built-in admin tool that can be powerful with a little code.

Creating an administrator account (Superuser)

Administrator account (Superuser) is can enter the website background, the data maintenance account, has a very high authority. Here we need to create an administrator account to add subsequent test data.

Create an administrator account by entering the python manage.py createsuperuser command:

(env) E:\django_project\my_blog>python manage.py createsuperuser
Username: dusai
Email address: [email protected]
Password:
Password (again):
Superuser created successfully.
Copy the code

The command will prompt you to enter your account name, email address and password.

Register ArticlePost in the background

Next we need to “tell” Django that we need to add the ArticlePost table in the background for administration.

Open article/admin.py and write the following code:

article/admin.py

from django.contrib import admin

Don't forget to import ArticlerPost
from .models import ArticlePost

# register ArticlePost with admin
admin.site.register(ArticlePost)
Copy the code

This is easy to register.

Roam in the background

As you may have noticed, Django projects are automatically configured with Settings and urls in the background, so you don’t need to worry about that.

Start the server, type http://127.0.0.1:8000/admin/ in your browser, everything is all right, you can see the login screen below:

Enter the administrator account you just created and log in:

In the red box is the ArticlePost data table you just added. Click “Enter” and then click “ADD ARTICLE POST” in the upper right corner to reach the following page:

If you look at each item in the form, you see that it matches the fields in ArticlePost exactly; Because the updated field specifies auto add, it is not shown here.

After filling out the form, click Save:

You can see that the ARTICLE POST contains the data you just entered. Write a few more pieces of data in the same way:

Preparations are now complete.

View the database

2018-10-29 New content

By doing this, we now have 1 user data and 3 article data in our database. Sometimes I need to check whether the data in the database is correct, but the database file db.sqlite3 in the project cannot be opened directly. What should I do?

This is where you need to use software that specializes in processing SQLite data files: SQLiteStudio

Download and install it, use it to open db.sqlite3, and a list of the various types of data saved in the database appears in the software navigation bar. For example, auth_user is the user data table:

You can use it to check that the database is working properly in your project code, which is very useful during the development phase.

conclusion

This chapter preliminarily felt the working mode of View and created Superuser to input several test data in the background.

The next chapter will write a more meaningful View, ready for the old driver to drive.

  • If you have any questions please leave a message on Doucet’s personal website and I will reply as soon as possible.
  • Or Email me a private message: [email protected]
  • Project code: Django_blog_tutorial

Please inform the author and indicate the source for reprinting.