【问题标题】:Django 1.11 Pagination MarkdownDjango 1.11 分页 Markdown
【发布时间】:2018-02-22 20:57:27
【问题描述】:

我尝试通过使用数组拆分它来在 Django 中对一个很长的字符串进行分页,我成功地做到了,但是 django-markdown-deux 停止工作。

我是这样实现的: 模型.py:

class Post(models.Model):
content = models.TextField()

def get_markdown(self):
    content = self.content
    markdown_text = markdown(content)
    return mark_safe(markdown_text)

views.py:

def post_detail(request, slug=None):  #retrieve
instance = get_object_or_404(Post, slug=slug)

#Detect the breaklines from DB and split the paragraphs using it
tempInstance = instance.content
PaginatedInstance = tempInstance.split("\r\n\r\n")

paginator = Paginator(PaginatedInstance, 5)  #set how many paragraph to show per page

page = request.GET.get('page', 1)

try:
    Paginated = paginator.page(page)
except PageNotAnInteger:
    Paginated = paginator.page(1)
except EmptyPage:
    Paginated = paginator.page(paginator.num_pages)

context = {
    "instance": instance,
    "Paginated": Paginated,  #will use this to display the story instead of instance (divided string by paragraph)
}

return render(request, "post_detail.html", context)

post_detail.html:

这是有效的(没有分页):

{{ instance.get_markdown }}

如果我删除 .get_markdown,则此文本将作为纯文本工作,如果我放入 .get_markdown 则不会显示任何内容

{% for paginatedText in Paginated %}
{{ paginatedText.get_markdown }}
{% endfor %}

【问题讨论】:

    标签: python django pagination markdown django-pagination


    【解决方案1】:

    您的paginatedText 实例没有定义get_markdown 方法。因此,当您尝试调用模板时,模板会静默失败。您需要改用markdown filter

    {% load markdown_deux_tags %}
    
    {% for paginatedText in Paginated %}
    {{ paginatedText|markdown }}
    {% endfor %}
    

    【讨论】:

    • 它有效,但是有时我使用降价表单插入的图像没有显示。
    • 每当我添加图像时,降价表单都会在我希望图像显示的位置生成此代码:![enter image description here][1],并且:` [1]: link-to-image.com \image.png` 位于内容的最后一部分,除非我将 `[1]: link-to-image.com\image.png` 复制到 ![enter image description here][1]
    • 对。分页器不支持 Markdown。它假定一个更简单的纯文本。您要么需要以分页器无法破坏的方式格式化 Markdown,要么可能使用更复杂的分页器。就个人而言,我建议将 Markdown 转换为 HTML 作为整个文档,然后对 HTML 进行分页。但是,这超出了这个问题的范围。
    猜你喜欢
    • 2017-10-28
    • 2018-01-19
    • 2017-12-19
    • 1970-01-01
    • 2018-05-07
    • 2018-01-09
    • 2018-09-02
    • 2017-10-28
    • 2018-03-12
    相关资源
    最近更新 更多