【发布时间】: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