매우 긴 이야기 (데이터베이스의 한 필드에서)를 포함 할 수있는 내 블로그 사이트를 만들려고합니다. 다른 뷰에서 레코드 목록 (스토리 목록 용)에 대한 페이지 매김을 성공적으로 만들고 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 %}
그러나 오류가 발생합니다.
마침내이 작업을 수행 할 수있는 해결 방법을 찾았습니다. 이것은 최상의 해상도는 아니지만 저에게 효과적입니다.
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 %}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다