When visiting the open source Chinese community, I found that each blog has a line of article labels, which are about the code types and modules used in the article. As shown in the following figure, my open source Chinese community address is: https://my.oschina.net/zhenfei.
I'd like to think of my own website instead, and I'd like to add such a function. In Django, then, it's very easy to associate articles with tags through the ManyToManyField field. Let's look at an example of an official document:
@python_2_unicode_compatibleclass Publication(models.Model): title = models.CharField(max_length=30) def __str__(self): return self.title @python_2_unicode_compatibleclass Article(models.Model): headline = models.CharField(max_length=100) publications = models.ManyToManyField(Publication, related_name='publications_article') def __str__(self): return self.headline
Use the shell to briefly demonstrate:
#Create publisher In [4]: p1 = Publication.objects.create(title='hello') #Create an article In [5]: a1 = Article.objects.create(headline='django') #At this time, the publisher and article have not been added to the publication article table correspondingly, and the get is empty In [6]: p1.publications_article.all() Out[6]: <QuerySet []> In [7]: p1.publications_article.add(a1) #Get articles published by this Publisher In [8]: p1.publications_article.all() Out[8]: <QuerySet [<Article: django>]> #Get the publisher who published this article In [9]: a1.publications.all() Out[9]: <QuerySet [<Publication: hello>]>
Let's start to implement this function in our own website.
1. Create a new Tag model class
from django.db import models class Tag(models.Model): """ //Tag is also simple, just like Category. //Again, we must inherit the model. Model class! """ name = models.CharField(max_length=100, verbose_name=u'label') class Meta: verbose_name = 'label' verbose_name_plural = 'label' def __str__(self): return self.name
2. Associate the article with the tag through ManyToManyField
In the Post model class of the article, add
tags = models.ManyToManyField(Tag, blank=True, verbose_name=u'label', related_name='tag_post')
3. Register the Tag model class in xadmin
import xadmin from .models import Tag class TagAdmin(object): """ //Role: custom label management tool admin.ModelAdmin:inherit admin.ModelAdmin class """ #Display od value and label name in the background list_display = ['id', 'name'] #Add filter box and use article classification as filter list_filter = ['name'] #Add article title search field search_fields = ['name'] #Background management displays 20 article titles per page xadmin.site.register(Tag, TagAdmin)
4. Define a template label
Please refer to: Django framework 13: custom filters and Tags.
from django import template register = template.Library() @register.simple_tag def get_post_tags(obj): """ Get all tags under the article param post: Blog return: tag list """ tags_list = obj.tags.all() return tags_list
5. Finally, load it in the web template
I omitted the specific CSS here, which is relatively simple.
{% get_post_tags post as tags_list %} {% if tags_list %} <div class="blog-tags"> {% for tag in tags_list %} <span>{{ tag }}</span> {% endfor %} </div> {% endif %}
This is done. The final display is as follows:
Source: https://www.jzfblog.com/detail/127 , the update and edit of the article shall be subject to this link. Welcome to the source station!