Hi
I like to show the five latest article on my index page. And I like to
have them either sorted by views or by date. So the user is allowed to
switch.
I tried this by implementing two session variables sw and sort. sort should
contain the how its currently sorted. sw contains the two options in a list.
default.py
def index():
rows=db().select(db.Article.ALL, orderby=~db.Article.Submitted, limitby
=(0,5)).sort(lambda row:row.Views, reverse=True)
rows2=db().select(db.Article.ALL, orderby=~db.Article.Submitted, limitby
=(0,5))
if not session.sortiert:
session.sw=["Views","Date"]
if not session.sort:
session.sort=session.sortiert[0]
if session.sort==session.sortiert[0]:
x=rows
elif session.sort==session.sortiert[1]:
x=rows2
else:
print "session.sort: ",session.sort," is an not accepted value"
return dict(Articles=x)
index.html
{{extend 'layout.html'}}
<h1>Index</h1>
<div class='Myheader'>
sort by
{{ if session.sort==session.sortiert[0]: }}
{{ session.sort=session.sortiert[1]}}
{{=A(session.sortiert[1],_href=URL(r=request,f='index')) }}
{{ elif session.sort==session.sortiert[1]: }}
{{ session.sort=session.sortiert[0]}}
{{=A(session.sortiert[0],_href=URL(r=request,f='index')) }}
{{pass}}
</div><br>
latest articles sorted by {{=session.sort}}:<br><br>
{{for i in Articles:}}
{{=i.Title}} <br>
{{pass}}
The swithching works even though its a little slow. But *sorted by* {{=s
ession.sort}} shows the same as *sort by* {{if statement above}}
If I'm not completly mistaken the logic says clearly it can never show the
same value.
Thanks guys
**