0

非常に長い話を含む自分のブログサイトを作成しようとしています(データベースの1つのフィールドから)。私は正常に私の他のビューの(ストーリーのリストのための)レコードのリストのページネーションを作成し、Djangoのドキュメントから実験しようとしました。私がしたのは、非常に長い文字列から配列を作成し、djangoページネーションがそれを数えられるようにすることです。非常に長い文字列のページ設定Django 1.11(Python 3.6)

「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) 

私は正常にそれを作成したが、全く良い見ていない代わりに、段落の書式の上から下への単語のリストとして。

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

をが、エラーを取得します。

"post_detail.html"

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

は、私はこれを使用して、それをconcatinateしようとしました。

+0

は私が作りたいテンプレートファイルにページ分割です次のようなものがあります:https://pagely.com/blog/2015/03/wordpress-auto-post-pagination/ –

答えて

0

最終的にこの問題を解決する方法が見つかりました。これは最高の解決策ではありませんが、私のために働きます。

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 %} 
0

はそれを試してください:

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

詳細templates join

+0

私はそれを試しましたが、同じですが、私はあなたが与えた文書をチェックしようとします。 –

+0

エラーコードを表示し、質問に追加してください。 –

+0

実際に表示されるエラーメッセージはありません。それはリストの外観をちょっと変えただけですが、以前と同じです。 –

1

私はあなたが正しい方法で行っていないと思います。コンテンツにページネーションを使用する代わりに、Ajaxを使用してより多くのコンテンツをロードすることができます。ロードすると、より多くのボタンが投稿のコンテンツを読み込みます。

コンテンツの流れはこのようなものになります。最初に500文字をロードした後、ユーザーがもっとボタンを押した後、ajax呼び出しを行い、次の500文字を持って前のコンテンツに追加します。等々。

+0

私はそれを試してみましょう。しかし、それが文書ファイルで行われる場合、コンテンツが50ページのように達する可能性があるので、改ページするのではなく、そうすることをお勧めしますか? –

+0

あなたの質問で言及したように、あなたのブログのコンテンツのページネーションが必要なのでしょうか?それから、私によれば、コンテンツを500文字ずつ徐々に500文字ずつ読み込んでください。あなたのコンテンツが50ページに収まるサイズよりもはるかに大きいと思うなら、1つまたは複数のAjaxリクエストをさらにロードした後で、キャラクターを最大1500文字まで増やすことができます。これを行う方法を知りたい場合は、私に知らせてください。ありがとう。 –

+0

はい。あなたは私がdjangoで申し込むことができる例を挙げていただけますか? –

関連する問題