If / else in Django View

时间:2017-08-30 20:55:10

标签: django django-templates django-views

I'm trying to display a different template depending on a condition:

https://api.instagram.com/v1/users/self/media/liked?access_token=ACCESS-TOKEN

If True - go to template 1.html If False - go to template 2.html

The above works for template 1.html only. I can't figure out how properly use the if/else statements to return the correct template and query_set for True/False. Feeling like a dope on this one.

3 个答案:

答案 0 :(得分:1)

you can override get, use NOT NULL for single query to the db

NOT NULL

and read all comments, they are useful

答案 1 :(得分:1)

get_queryset()应该返回一个QuerySet对象,因此解决方案是返回一个空的QuerySet,您可以使用none() function。{/ p>

因此,返回DailyRoute.objects.none()应该为模板2执行此操作。

答案 2 :(得分:0)

如果您没有向模板发送任何内容,那么您也可以在满足条件时使用HttpResponseRedirect重定向到template2。