【问题标题】:Pagination for very long string Django 1.11 (Python 3.6)非常长的字符串 Django 1.11 (Python 3.6) 的分页
【发布时间】:2018-01-19 10:06:10
【问题描述】:

我正在尝试创建自己的博客站点,该站点可能包含一个很长的故事(来自数据库中的一个字段)。我成功地在我的其他视图上为记录列表(故事列表)创建了分页,并尝试从 Django 文档中进行试验。我所做的是从很长的字符串创建一个数组,以便 django 分页可以计算它。

“views.py”

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

words_list = instance.content.split()
paginator = Paginator(words_list, 500)  # Show 25 contacts per page

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

try:
    words = paginator.page(page)
except PageNotAnInteger:
    # If page is not an integer, deliver first page.
    words = paginator.page(1)
except EmptyPage:
    # If page is out of range (e.g. 9999), deliver last page of results.
    words = paginator.page(paginator.num_pages)

if instance.draft or instance.publish > timezone.now().date():
    if not request.user.is_staff or not request.user.is_superuser:
        raise Http404
share_string = urlquote_plus(instance.content)
context = {
    "title": instance.title,
    "instance": instance,
    "share_string": share_string,
    "word_content": words,
}

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

我成功创建了它,但它是一个从上到下的单词列表,而不是看起来一点都不好看的段落格式。

“post_detail.html”

{% for word_con in word_content %}
            <p class="text-justify">{{ word_con }}</p>
{% endfor %}

我试图用这个来连接它:

{% for word_con in word_content %}
            <p class="text-justify">{{ ' '.join(word_con) }}</p>
{% endfor %}

但出现错误。

【问题讨论】:

标签: django python-3.x django-templates django-views django-pagination


【解决方案1】:

我认为你的做法不对。您可以使用 Ajax 加载更多内容,而不是使用分页来加载内容,加载更多按钮将加载您帖子的内容。

内容的流程是这样的,首先加载 500 个字符,然后在用户按下加载更多按钮后,您执行 ajax 调用并带来下一个 500 个字符并附加到以前的内容。等等。

【讨论】:

  • 我会试试的。但是,如果在文档文件中完成内容可能会达到 50 页,是否建议这样做而不是进行分页?
  • 正如您在问题中提到的,您需要为您的博客内容分页,对吗?然后根据我的说法,您应该通过逐步加载 500 个字符到 500 个字符的内容来做到这一点。如果您认为您的内容比 50 页大得多,那么您可以在一个或多个加载更多 ajax 请求后增加您的字符(最多 1500 个字符)。如果您想知道如何执行此操作,请告诉我。谢谢。
  • 是的。你能举一个我可以在django中应用的例子吗?
【解决方案2】:

我终于找到了一种解决方法来完成这项工作。这不是最佳分辨率,但对我有用。

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 = {
    "Paginated": Paginated,  #will use this to display the story instead of instance (divided string by paragraph)
}

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

我决定不计算所有字符,而是按段落拆分字符串并将其保存在一个数组中,这就是我在模板文件中分页的那个

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

【讨论】:

    猜你喜欢
    • 2017-10-11
    • 2014-12-19
    • 1970-01-01
    • 1970-01-01
    • 2018-05-07
    • 2018-02-22
    • 2019-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多