HelloDjango Article 11: Automatically Generating Article Summaries

Author: Hello GitHub - Dream chaser

The sample code involved in this article has been synchronously updated to HelloGitHub-Team repository

The model for blog posts has an excerpt field, which stores a summary of the posts.Until now, you can only manually enter summaries for articles in the django admin background.Each time you enter a summary manually, it can be cumbersome. For some articles, simply extract the first N characters of the body as the summary to provide a preview of the article.So we implement that if the article does not enter a summary, automatically extract the first N characters of the body as the summary, which has two implementations.

Override save method

The first method is to save the first N characters from the body segment to the summary field by overriding the save method of the model.stay Creation background open, please start your performance We mentioned that the save method performs the logic of saving model instance data to the database, so by overriding the save method, do something before saving the database, such as populating the value of a missing field.

Review the blog post model code:

blog/models.py

class Post(models.Model):
    # Other fields...
    body = models.TextField()
    excerpt = models.CharField(max_length=200, blank=True)
    
    def save(self, *args, **kwargs):
        self.modified_time = timezone.now()
        super().save(*args, **kwargs)

The body field stores the body, and the excerpt field stores the summary.By overriding the save method of the model, N characters are extracted from the body field and saved in the excerpt field before the data is saved in the database, thus achieving the purpose of automatic summarization.The code is as follows:

blog/models.py

import markdown
from django.utils.html import strip_tags

class Post(models.Model):
    # Other fields...
    body = models.TextField()
    excerpt = models.CharField(max_length=200, blank=True)
    
    # Other methods...
    
    def save(self, *args, **kwargs):
        self.modified_time = timezone.now()

        # First instantiate a Markdown class to render the body's text.
        # Since summaries do not require article catalogs to be generated, catalog expansion is removed.
        md = markdown.Markdown(extensions=[
            'markdown.extensions.extra',
            'markdown.extensions.codehilite',
        ])

        # Render Markdown text as HTML text first
        # strip_tags removes all HTML tags from HTML text
        # Assign excerpt the first 54 characters from the text
        self.excerpt = strip_tags(md.convert(self.body))[:54]

        super().save(*args, **kwargs)

The scheme for generating the summary here is to first convert the Markdown text in the body into HTML text, remove the HTML tags from the HTML text, and then extract the first 54 characters of the text as the summary.The goal of removing HTML tags is to prevent block-level HTML tags from appearing in the first 54 characters and making the summary format ugly.You can see that many websites use this method of generating summaries.

Then use the template tag to reference {{post.excerpt}} where appropriate in the template to display the summary value:

templates/blog/index.html

<article class="post post-{{ post.pk }}">
  ...
  <div class="entry-content clearfix">
      <p>{{ post.excerpt }}...</p>
      <div class="read-more cl-effect-14">
          <a href="{{ post.get_absolute_url }}" class="more-link">Continue reading <span class="meta-nav">→</span></a>
      </div>
  </div>
</article>

You can see the summary effect by adding a new article (which triggers the save method, since the previously added article does not automatically generate a summary. Save the trigger save method manually).

Using truncatechars template filters

The second method is to use the truncatechars template Filter.In django's template system, the usage syntax of the template Filter is {{var | filter: arg}}.You can think of template filtering as a function that acts on the template variables it filters, thereby changing the value of the template variables.For example, the truncatechars Filter here intercepts the first N characters of the template variable value.For template filters, we have previously used the safe Filter for reference Let blogs support Markdown syntax and code highlighting The description of template filters in this article.

For example, if the summary effect requires the first 54 characters of post.body to be displayed, you can use {{post.body | truncatechars:54} in the template.

templates/blog/index.html

<article class="post post-{{ post.pk }}">
  ...
  <div class="entry-content clearfix">
      <p>{{ post.body|truncatechars:54 }}</p>
      <div class="read-more cl-effect-14">
          <a href="{{ post.get_absolute_url }}" class="more-link">Continue reading <span class="meta-nav">→</span></a>
      </div>
  </div>
</article>

One drawback of this approach, however, is that if the first 54 characters contain block-level HTML element tags, such as a block of code, the summary will look ugly.Therefore, the first method is recommended.

Welcome to the HelloGitHub Public Number for more information and content on open source projects

Explain Open Source Projects Series Launch - Let people who are interested in open source projects stop fearing and the sponsors of open source projects stop being alone.Follow our articles and you'll find it so easy to program, use, and participate in open source projects.Welcome to contact us to submit a contribution for more people to love and contribute to Open Source.

Tags: Python Database Django github

Posted on Wed, 28 Aug 2019 19:11:10 -0700 by anhedonia