Pycharm + Django tutorial 2- actual hands-entry application creates

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/baidu_39459954/article/details/90233039

Create a voting application

If after following the completion of an on your development environment - a "project" - is already configured, you can start to work.

In Django, each application package is a Python, and follows the same convention. Django comes with a tool that can help you generate a basic directory structure of the application, so you can concentrate on writing code rather than creating a catalog.

Project VS application
programs and applications what's the difference? It is a specialized application to do something web applications - such as blog system, or database of public records or a simple voting procedure. Project is a collection of configurations and applications of a Web site. Project can contain a number of applications. Application can be used by a number of projects.

Your application can be stored in the path defined in any Python path. In this tutorial, we will be in your manage.py voting applications in the same directory. So that it can import as a top-level module, instead of the sub-module mysite.

Click Tools, open the Run manage.py Task
Here Insert Picture Description
input startapp polls, polls create applications
Here Insert Picture Description
that will create a directory polls, its directory structure is as follows:
Here Insert Picture Description

Writing your first view

Let us begin to write your first view of it. Open polls / views.py, the following code is entered into the Python:

from django.http import HttpResponse
    
def index(request):
    return HttpResponse("Hello, world. You're at the polls index.")

This is the most simple Django view. If you want to see results, we need to map a URL to it - that's why we need URLconf of the.

To create a URLconf, create a new directory in the polls urls.py file. Your application directory should now look like this:

In polls / urls.py, enter the following code:

from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='index'),
]

The next step is to specify polls.urls module we created in the root URLconf file. Insert a urlpatterns include in the list mysite / urls.py file in (), as follows:

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

urlpatterns = [
    path('polls/', include('polls.urls')),
    path('admin/', admin.site.urls),
]

Function include () allows references to other URLconfs. Whenever Django encountered: func: ~ django.urls.include, it truncates the URL matches this portion, and to transmit the remaining string URLconf for further processing.

Design concepts include () is that it can plug and play. Because voting application has its own URLconf (polls / urls.py), they can be placed in "/ polls /", "/ fun_polls /", "/ content / polls /", or any other path, the applications to work properly.

用你的浏览器访问 http://127.0.0.1:8000/polls/,
你应该能够看见 “Hello, world. You’re at the polls index.” ,这是你在 index 视图中定义的。
Here Insert Picture Description
函数 path() 具有四个参数,两个必须参数:route 和 view,两个可选参数:kwargs 和 name。现在,是时候来研究这些参数的含义了。
path() 参数: route
route 是一个匹配 URL 的准则(类似正则表达式)。当 Django 响应一个请求时,它会从 urlpatterns 的第一项开始,按顺序依次匹配列表中的项,直到找到匹配的项。

这些准则不会匹配 GET 和 POST 参数或域名。例如,URLconf 在处理请求 https://www.example.com/myapp/ 时,它会尝试匹配 myapp/ 。处理请求 https://www.example.com/myapp/?page=3 时,也只会尝试匹配 myapp/。

path() 参数: view
当 Django 找到了一个匹配的准则,就会调用这个特定的视图函数,并传入一个 HttpRequest 对象作为第一个参数,被“捕获”的参数以关键字参数的形式传入。稍后,我们会给出一个例子。

path() 参数: kwargs
任意个关键字参数可以作为一个字典传递给目标视图函数。本教程中不会使用这一特性。

path () Parameters: name
the name makes you uniquely refer to it anywhere in Django, especially in the template for your URL. This useful feature allows you to change just one file will be able to globally modify a URL pattern.

When you understand the basic request and response process after a configuration database read.

Previous: Pycharm + Django hands-on projects to create entry-combat tutorial 1-

Next: + Django hands-entry Pycharm combat Lesson 3 database and model

Guess you like

Origin blog.csdn.net/baidu_39459954/article/details/90233039