Python Project Practice III (Web application) Chapter III

Source: Internet
Author: User
Tags virtual environment

Continue with the previous section, and now you want to show pages for all topics

With efficient page creation, you can focus on two other pages: a Web page that displays all the topics, and a page that shows items in a particular topic. All theme pages display all the topics created by the user, which is the first web page that needs to use the data.

A page 1 URL pattern showing all topics
#定义learning_logs的URL模式from django.conf.urls import Urlfrom. Import viewsapp_name= ' learning_logs ' urlpatterns=[    #主页    url (r ' ^$ ', views.index,name= ' index '),    #显示所有主题,    URL (r ' ^topics/$ ', views.topics,name= ' topics ')    ]
2 views

The function topics () needs to fetch some data from the database and send it to the template. The code we need to add in views.py is as follows:

From django.shortcuts import renderfrom. Models import topic# Create your views Here.def index (Request): "    home page of study notes" '    return render (Request, ' learning_logs/index.html ') def topics (Request):    topics = Topic.objects.order_by (' Date_added ')    context = {' Topics ': topics}    return render (Request, ' learning_logs/topics.html ', context)
3 templates

Templates that display pages for all topics accept the dictionary context so that you can use the data provided by topics (). Please create a file, name it topics.html, and store it in the same directory as the index.html. The following shows how to display a topic in this template:

{% extends "learning_logs/base.html"%} {% block content%}<p>topics</p><ul>  {% for topic in Topics%}    <li>{{topic}}</li >  {% empty%}    <li>no topics has been added yet.</li> {% endfor%}</ul>{% endblock content%}

Open this link:http://127.0.0.1:8000/topics/.

Two pages showing a specific topic

Next, we need to create a page that focuses on a particular topic--Display the name of the topic and all the entries for that topic. Again, we'll define a new URL pattern, write a view and create a template. We will also modify the page that displays all the topics so that each item list item is a link, and clicking it will display all the entries for that topic.

1 URL pattern

The URL pattern for a page that displays a specific topic differs slightly from all previous URL patterns because it uses the subject's ID attribute to indicate which topic is requested. For example, if a user wants to view a detailed page with the subject chess (whose ID is 1), the URL will be http://localhost:8000/topics/1/. Here is the pattern that matches this URL, which is included in the learning_logs/urls.py:

#定义learning_logs的URL模式from django.conf.urls import Urlfrom. Import viewsapp_name= ' learning_logs ' urlpatterns=[    #主页    url (r ' ^$ ', views.index,name= ' index '),    #显示所有主题,    URL (r ' ^topics/$ ', views.topics,name= ' topics '),    #特定主题的详细页面    url (r ' ^topics/(? p<topic_id>\d+) ', views.topics,name= ' topic ')    ]

When the URL is found to match this pattern, Django invokes the View function topic () and passes the value stored in topic_id as an argument to it. In this function, we will use the value of topic_id to get the corresponding topic.

2 views

The function topic () needs to get the specified topic from the database and all the entries associated with it, as follows:

def topic (request,topic_id): "Displays a single theme and all Entries" Topic=topic.objects.get (id=topic_id) Entries=topic.entry_ Set.order_by ('-date_added ') context = {' topic ': topic, ' Entries ': Entries}return render (Request, ' learning_logs/ Topic.html ', context)
3 templates

This template needs to display the name of the subject and the content of the entry, and if the current topic does not contain any entries, we also need to point this out to the user:

{% extends "learning_logs/base.html"%} {% block content%}<p>topic: {{Topic}}</p><p>entries:</p><ul>{% for entry in Entries%}
   <li>    <p>{{entry.date_added|date: ' M D, Y h:i '}}</p>    <p>{{Entry.text|linebreaks} }</p>  </li>{% Empty%}  <li>  There is no entries for this topic yet.  </li>{% endfor%}</ul>{% endblock content%}

Results such as:

Three summary

In this chapter, we first learned how to use the Django framework to create a Web application. A brief project specification was developed, Django was installed in the virtual environment, a project was created, and the project was verified to be created correctly. Learn how to create an application and how to define a model that represents your application's data. Learn about the database, and what help Django can do to migrate the database after the model has been modified. Learn how to create a superuser who can access the administration site and enter some initial data using the Administration Web site. It also explores the Django shell, which allows you to work with project data in a terminal session. Learned how to define URLs, create view functions, and write templates for creating Web pages for Web sites. Finally, template inheritance is used, which simplifies the structure of each template and makes it easier to modify the site.

Four perspectives

In the next study, we will create a user-friendly and intuitive Web page that allows users to add new topics and items without having to manage the site, as well as edit existing entries. We will also add a user registration system that allows users to create accounts and their own learning notes. Making it possible for any number of users to interact with it is at the heart of your Web application.

Python Project Practice III (Web application) Chapter III

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.