Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Behr, Svenja
Django_Blog
Commits
16495a78
Commit
16495a78
authored
Jun 29, 2020
by
Tang, Alexander
Browse files
Anzeige der Blogeinträge nun wie folgt: letzten 3 zuerst, alle anderen nach Zufallsprinzip
parent
fcc3c100
Changes
1
Hide whitespace changes
Inline
Side-by-side
blog/views.py
View file @
16495a78
...
...
@@ -2,6 +2,10 @@ from django.http import HttpResponseNotAllowed, Http404
from
django.contrib
import
messages
from
django.shortcuts
import
render
,
get_object_or_404
,
redirect
from
django.urls
import
reverse_lazy
from
django.db.models
import
Q
from
itertools
import
chain
from
random
import
shuffle
from
.forms
import
CommentForm
from
.models
import
*
...
...
@@ -15,7 +19,14 @@ def all_blogs(request):
"""
# Exclude those with status hidden from the overall list
query
=
f
'status &
{
BLOG_STATE_HIDE_FROM_LIST
}
<>
{
BLOG_STATE_HIDE_FROM_LIST
}
'
blogs
=
Blog
.
objects
.
extra
(
where
=
[
query
]).
order_by
(
'created'
)
noOfBlogs
=
Blog
.
objects
.
filter
(
~
Q
(
status
=
1
)).
count
()
# 2 queries turned into lists for shuffling, then being chained
lastThreeBlogs
=
list
(
Blog
.
objects
.
extra
(
where
=
[
query
]).
order_by
(
'-date'
)[:
3
])
allBlogsExceptLastThree
=
Blog
.
objects
.
filter
(
~
Q
(
status
=
1
)).
order_by
(
'date'
)[:(
noOfBlogs
-
3
)]
allBlogsExceptLastThreeInRandomOrder
=
list
(
allBlogsExceptLastThree
)
shuffle
(
allBlogsExceptLastThreeInRandomOrder
)
blogs
=
chain
(
lastThreeBlogs
,
allBlogsExceptLastThreeInRandomOrder
)
return
render
(
request
,
'blog/all_blogs.html'
,
{
'blogs'
:
blogs
})
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment