HTML 中的计数器?
我正在使用 Google App 引擎,在 html 文件中我想向不同的用户显示不同的屏幕,即
- 他们尚未登录,请向他们显示登录屏幕
- 他们已登录,这是第一次使用该系统
- 他们是回头客
到目前为止,我的代码中的循环如下所示
{% 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 %}
是否有无论如何要增加计数器for 循环如注释中所示,然后可以检查,如果为 0 那么用户已经不在数据库中?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,循环遍历模板中的所有用户并不是最好的主意。最好在
视图
中使用数据库过滤器
查询来查找用户,并设置适当的变量来确定模板中的操作。话虽如此,获得 forloop 计数器是是可能的。在 for 循环内部,它由forloop.counter
(从 1 开始计数)或forloop.counter0
(从 0 开始计数)给出。您可以在 Django 文档。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 yourview
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 byforloop.counter
(counts from 1) orforloop.counter0
(counts from 0). You can read more about it in the Django docs.您应该考虑改变您的重定向方式。在模板中迭代数据库中的所有用户并不是一个好主意,您应该编写一个过滤器或将逻辑移动到控制器文件中。装饰器将为您派上用场。对于您的问题,您无法在模板内设置计数器。如果您愿意,可以使用默认值
for 循环变量。我认为这对你没有任何用处。
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.
您应该在未经过身份验证的用户的视图上使用装饰器。尝试 @login_required 装饰器。
对于登录计数,最好为您的 个人资料模型,例如login_counts。然后,您可以在用户登录时随时增加此值。获取正确的登录信号可能有点棘手,因为可能会出现重定向、登录失败和其他问题。有关更多详细信息,请参阅此帖子
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