【问题标题】:How to truncate content for Mdeditor (content as markdownx) - Django如何截断 Mdeditor 的内容(内容为 markdownx) - Django
【发布时间】:2021-01-11 01:50:22
【问题描述】:

我安装了 Mdeditor,一切正常。我的问题是当我想在模板中截断 BlogPost 的内容时,它的外观就是这样。

这是我的模型 blog/models.py

class BlogPost(models.Model): # blogpost_set -> queryset
    # id = models.IntegerField() # pk
    user    = models.ForeignKey(User, default=1, null=True, on_delete=models.SET_NULL)
    image   = models.ImageField(upload_to='image/', blank=True, null=True, height_field='height_field', width_field='width_field')
    height_field = models.PositiveIntegerField(default=0)
    width_field = models.PositiveIntegerField(default=0)
    title  = models.CharField(max_length=120)
    slug   = models.SlugField(unique=True) # hello world -> hello-world
    content  = MDTextField(default=' ')
    # content  = models.TextField(null=True, blank=True)
    publish_date = models.DateTimeField(auto_now=False, auto_now_add=False, null=True, blank=True)
    timestamp = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    category = models.CharField(max_length=255, default='issue')
    private = models.BooleanField(default = False)
    tags = TaggableManager()
    
    objects = BlogPostManager()

这是图片中页面的模板代码:

<article class="blog-item heading_space wow fadeIn text-center text-md-left" data-wow-delay="300ms">
    <div class="image"> <img src="{{ blog_post.image.url }}" alt="blog" class="border_radius"></div>
    <h3 class="darkcolor font-light bottom10 top30"> <a href="{{ blog_post.get_absolute_url }}">{{ blog_post.title|capfirst }}</a></h3>
    <ul class="commment">
        <li><a href="#."><i class="fas fa-calendar"></i>{{ blog_post.publish_date }}</a></li>
        <!-- <li><a href="#."><i class="fas fa-comments"></i></a></li> -->
        <li><a href="#."><i class="fas fa-user"></i>{{ blog_post.user }}</a></li>
    </ul>
    <!-- <p class="top15">blog_post.content|linebreaks|truncatewords:50</p> -->
    <div id="content"><textarea>{{blog_post.content}}</textarea>
    </div>
</article>

有没有办法让它显示带有 truncatewords 的内容作为降价?或者像降价一样看起来不错的东西?

文档:

【问题讨论】:

    标签: python django markdown django-markdownx


    【解决方案1】:

    我找到了解决办法。

    我在我的模板中使用了这个:

    <div id="content"><textarea> {{blog_post.content|truncatechars_html:500}} </textarea></div>
    

    【讨论】:

      猜你喜欢
      • 2023-03-23
      • 1970-01-01
      • 2017-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多