Reputation: 509
I am using Google App engine, in the html file I want to show different screens to different users, ie,
- they are not logged in the show them a log in screen
- They are logged in and this is there first time to use the system
- They are a returning user
So far the loops in my code is as follows
{% ifequal access_token None %}
<!-- Log in user-->
{% else %}
{% ifequal user_set None %}
<!-- First ever user in DB -->
{% else%}
{% for user in user_set %}
{% ifequal user.session_ID access_token %}
<a href="/logout">Logout {{user.user_name}}</a>
{% else %}
<!-- add in counter? -->
{%endifequal%}
{% endfor %}
{% endifequal%}
{% endifequal %}
Is there anyway to increment a counter in the for loop as shown in the comments which could then be checked and if 0 then the user is not in the db already?
Upvotes: 2
Views: 3341
Reputation: 95
You should use decorators on views for users that are not authenticated. try the @login_required decorator.
For login counts, it better to create a database integerfield for your profile model, say login_counts. You then increase this anytime the user logs in. Getting the right login signal can be a bit tricky as there can be redirects and failed logins and other stuff. See this post for more details
Upvotes: 1
Reputation: 5842
You should consider changing your way of redirection . It's not a good idea to iterate through all users in the db in the templates, Rather you should write a filter or move the logic to the controllers files. A decorator will come in handy for you. Coming to your question, You cannot set a counter inside template. If you want you can use the default for loop variables. I don't think it will be of any use to you.
forloop.counter The current iteration of the loop (1-indexed)
forloop.counter0 The current iteration of the loop (0-indexed)
forloop.revcounter The number of iterations from the end of the loop (1-indexed)
forloop.revcounter0 The number of iterations from the end of the loop (0-indexed)
forloop.first True if this is the first time through the loop
forloop.last True if this is the last time through the loop
forloop.parentloop For nested loops, this is the loop "above" the current one
Upvotes: 1
Reputation: 48445
Firstly, it's not the greatest idea to loop through all your users in your template. It would be better to use a database filter
query in your view
to find the user, and set an appropriate variable to determine the action in your template. That being said, it is possible to get a forloop counter. Inside the forloop, it's given by forloop.counter
(counts from 1) or forloop.counter0
(counts from 0). You can read more about it in the Django docs.
{% for user in user_set %}
{% ifequal user.session_ID access_token %}
<a href="/logout">Logout {{user.user_name}}</a>
{% else %}
<!-- add in counter? -->
{{forloop.counter}} <!-- current count -->
{%endifequal%}
{% endfor %}
Upvotes: 3