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