Django Quick Build Blog Tenth (fix home page, read amount of data)

Source: Internet
Author: User
Tags auth sort
writing here, we have already used the Django Blog Foundation Development Framework and so on to develop the end, the next is the Django advanced stage, the difficulty will be slightly larger.

Here are some of the main things that are missing out:

1 Click events on the homepage of our blog are not implemented

2 reading volume of the article is not filled for 1th:

We just need to fill in the corresponding Click event to be good:
1. Add a click event at Snake & Baby and home page:

/blog/base.html

<li class= "cl-effect-11" ><a href= "{% url ' blog:index '%}" data-hover= "Home" > Home </a></li>

2. Add statistical data to the comment book:
Because the article content is the most recent article shows in the front, so we have to follow the time to obtain the article, the use of the syntax is Post.objects.all (). order_by ('-created_time ') , which leads to many repetitions, So we simply make a natural sort of post in the post model, which is similar to implementing the comparable interface in Java, and all we need to do is define an internal class meta class within the post and specify the sort properties.

From django.db import models from django.contrib.auth.models import User from django.urls import reverse from django.utils
. Six import python_2_unicode_compatible # python_2_unicode_compatible adorner for compatibility with Python2 @python_2_unicode_compatible Class Category (models. Model): "" Django requires that the models must inherit models.
    Model class.
    Category requires only a simple category name name.
    Charfield Specifies the data type of the class name name, Charfield is the character type, Charfield's max_length parameter specifies its maximum length, and the class name beyond that length cannot be stored in the database.
    Of course, Django also provides us with a number of other data types, such as datetime type Datetimefield, integer type Integerfield, and so on. All Django built-in types can view documents: https://docs.djangoproject.com/en/1.10/ref/models/fields/#field-types "" "" Name = MODELS.C Harfield (max_length=100) def __str__ (self): return Self.name @python_2_unicode_compatible class Tag (models.
    Model): "" "tag tag is also relatively simple, and Category. Again, we must inherit models.
    Model class. "" "Name = models. Charfield (max_length=100) def __str__ (self): return Self.name @python_2_unicode_compatible class Post (MOdels.
    Model): "" "the database table of the article is slightly more complex, mainly involving more fields. "" "# Article title = models.
    Charfield (max_length=70) # article text, we used the TextField.
    # Storing shorter strings can use Charfield, but may be a large piece of text for the body of the article, so use TextField to store large pieces of text. BODY = models.
    TextField () # These two columns represent the time the article was created and the last time it was modified, with the Datetimefield type for the stored Time field. Created_time = models. Datetimefield () Modified_time = models.
    Datetimefield () # Article Summary, there can be no article summary, but by default Charfield requires that we have to deposit data, otherwise it will be an error.
    # you can allow null values after specifying the Blank=true parameter value for Charfield. Excerpt = models.
    Charfield (max_length=200, blank=true) # This is the classification with labels, categories and tags of the model we have defined above.
    # Here we associate the database tables of the article with the database tables that correspond to the categories and tags, but the association forms are slightly different.
    # We require an article to correspond to only one category, but a category can have multiple articles, so we are using ForeignKey, a one-to-many relationship.
    # and for labels, an article can have multiple labels, and there may be multiple articles under the same label, so we use Manytomanyfield to show that this is a many-to-many relationship.
    # at the same time we stipulate that the article can have no tags, so the tag tags are specified blank=true. # If you don't know ForeignKey and Manytomanyfield, see the explanations in the tutorial, and also refer to the official documentation: # https://docs.djangoproject.com/en/1.10/topics/db/models/ #relationships category = models. FoReignkey (Category) tags = models.
    Manytomanyfield (Tag, blank=true) # article author, here User is imported from Django.contrib.auth.models.
    # Django.contrib.auth is a Django built-in app designed to handle the process of registering and logging on to website users, which is a user model that Django has written for us.
    # Here we associate the article with the User through ForeignKey.
    # because we stipulate that an article can have only one author, and an author may write multiple articles, this is a one-to-many association, similar to Category. Author = models. ForeignKey (User) def __str__ (self): return Self.title # Custom Get_absolute_url Method # Remember to guide from Django.urls Into the reverse function Def get_absolute_url (self): return reverse (' blog:detail ', kwargs={' PK ': self.pk}) Class Meta
 : # Ordering used to specify the sort of article, ordering = ['-created_time ']

The above is mainly added: Class Meta:
# Ordering is used to specify the sort of articles,
ordering = ['-created_time '] so you can delete the code in the view function that sorts the returned results in the list of articles.

Next is the Django advanced stage, with some tinkering with new features

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.