Python Project Practice III (Web application) second article

Source: Internet
Author: User

Following the continuation of the previous section, the process of creating a Web page with Django typically has three stages: defining URLs, writing views, and writing templates. First, you must define the URL pattern, where each URL is mapped to a specific view-the view function gets and processes the data needed for the page. A view function typically invokes a template that generates a Web page that the browser can understand. To understand how it works, let's create a home page for learning notes. We'll define the URL of the home page, write its view function, and create a simple template.

Create a Web page: Learning Notes Home

1 Mapping URLs

The user requests the Web page by entering a URL in the browser and clicking the link, so we need to determine which URLs are required for the project. The URL of the home page is the most important, which is the underlying URL that the user uses to access the project. Currently, the base URL (http://localhost:8000/) returns to the default Django site, letting us know that the project was built correctly. We will revise this and map this base URL to the home page of the learning notes. Open the file urls.py in the Project home folder Learning_log, and you will see the following code:

From django.conf.urls import include,urlfrom django.contrib import adminfrom django.urls Import pathurlpatterns = [    P Ath (' admin/', admin.site.urls),    url (r ', include (' Learning_logs.urls ', namespace= ' learning_logs ')),]

The default urls.py is included in the folder Learning_log, and now we need to create another urls.py file in the folder Learning_logs:

#定义learning_logs的URL模式from django.conf.urls import Urlform. Import viewsurlpatterns=[    #主页    url (r ' ^$ ', views.index,name= ' index ')    ]

The actual URL pattern is a call to the function URL (), which accepts three arguments. The first one is a regular expression. Django looks for a regular expression in urlpatterns that matches the requested URL string, so the regular expression defines the pattern that Django can find.

The second argument of a URL () specifies the view function to invoke. When the requested URL matches the preceding regular expression, Django calls Views.index (this view function will be written in the next section). The third real arguments the name of this URL pattern is specified as index, allowing us to refer to it elsewhere in the code. Whenever we need to provide a link to this page, we will use this name instead of writing the URL.

2 Writing a View

The view function accepts the information in the request, prepares the data needed to generate the Web page, and then sends the data to the browser-often using a template that defines what the Web page is.
The file views.py in Learning_logs is automatically generated when you execute the command Python manage.py Startapp, and the current contents are as follows:

From django.shortcuts Import Render

Currently, this file only imports the function render (), which renders the response based on the data provided by the view. The following code shows how to write a view for the home page:

From django.shortcuts import render# Create your views Here.def index (Request):    "The home page of the Learning note" '    return render ( Request, ' learning_logs/index.html ')

3 Writing a template

A template defines the structure of a Web page. The template specifies what the page is, and Django fills in the relevant data whenever the page is requested. Templates give you access to any data that the view provides. Our home Page view does not provide any data, so the template is very simple.

<p>learning log</p><p>learning Log helps you keep track of your learning, for any topic you ' relearning a Bout.</p>

If you request the base url--http://localhost:8000/for this project, you will see the page you just created, not the default Django page. Django accepts the requested URL and discovers that the URL matches the pattern R ' ^$ ', so call function Views.index (), which will render the page using the index.html contained template, resulting

Tip: One of the questions above is to report an error:

  Path (R ', include (' Learning_logs.urls ', namespace= ' learning_logs ')),  File "D:\study\python\code\learning_log\ 11_env\lib\site-packages\django\urls\conf.py ", line up, in include    ' specifying a namespace in include () without Providing an app_name ' django.core.exceptions.ImproperlyConfigured:Specifying a namespace in include () without Providing an app_name are not supported. Set the App_name attribute in the included module, or pass a 2-tuple containing the list of patterns and app_name instead.

Be sure to change the urls.py below D:\study\python\code\learning_log\learning_log to the following:

From django.conf.urls import include,urlfrom django.contrib import adminfrom django.urls Import pathurlpatterns = [    P Ath (' admin/', admin.site.urls),    path (R ', include (' Learning_logs.urls ', namespace= ' learning_logs ')),]

And in D:\study\python\code\learning_log\learning_logs below the urls.py Plus: app_name= ' Learning_logs ' as follows:

#定义learning_logs的URL模式from django.conf.urls import Urlfrom. Import viewsapp_name= ' learning_logs ' urlpatterns=[    #主页    url (r ' ^$ ', views.index,name= ' index ')    ]

Second create other web pages

Once you've created the process for creating a Web page, you can begin to expand your "Learning notes" project. We will create two Web pages displaying data, one listing all the topics, and the other showing all the entries for a particular topic. For each page, we will specify the URL pattern, write a view function, and write a template. But before we do this, we create a parent template, and the other templates in the project inherit it.

1 Template Inheritance

Parent Template:

Let's start by creating a template named Base.html and storing it in the directory where index.html resides. This file contains all the elements of the page, and the other templates inherit base.html. Currently, all pages contain only the top header of the element. We'll include this template on each page, so we'll set this heading to a link to the home page:

<p>  <a href= "{% url ' learning_logs:index '%}" >learning log</a></p>{% block content%}{% Endblock content%}

2 Sub-templates

Now you need to rewrite the index.html to inherit the base.html as follows:

{% extends "learning_logs/base.html"%} {% block content%}  <p>learning Log helps you keep track of your learning, for any topic you ' re  learning about.</p>{% endblock Content%}

Today you have something to write about here, continue to study tomorrow!

Python Project Practice III (Web application) second article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.