Django 1.11 分页降价
Django 1.11 Pagination Markdown
我试图在 Django 中对一个很长的字符串进行分页,方法是使用我成功完成的数组将其拆分,但是 django-markdown-deux
停止工作。
以下是我的实现方式:
models.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 %}
您的 paginatedText
实例没有定义 get_markdown
方法。因此,当您尝试调用模板时,模板会静默失败。您将需要使用 markdown
filter 代替:
{% load markdown_deux_tags %}
{% for paginatedText in Paginated %}
{{ paginatedText|markdown }}
{% endfor %}
我试图在 Django 中对一个很长的字符串进行分页,方法是使用我成功完成的数组将其拆分,但是 django-markdown-deux 停止工作。
以下是我的实现方式: models.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 %}
您的 paginatedText
实例没有定义 get_markdown
方法。因此,当您尝试调用模板时,模板会静默失败。您将需要使用 markdown
filter 代替:
{% load markdown_deux_tags %}
{% for paginatedText in Paginated %}
{{ paginatedText|markdown }}
{% endfor %}