HTML 中的计数器?

发布于 2024-11-04 11:38:15 字数 851 浏览 3 评论 0原文

我正在使用 Google App 引擎,在 html 文件中我想向不同的用户显示不同的屏幕,即

  1. 他们尚未登录,请向他们显示登录屏幕
  2. 他们已登录,这是第一次使用该系统
  3. 他们是回头客

到目前为止,我的代码中的循环如下所示

{% 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,

  1. they are not logged in the show them a log in screen
  2. They are logged in and this is there first time to use the system
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

宫墨修音 2024-11-11 11:38:15

首先,循环遍历模板中的所有用户并不是最好的主意。最好在视图中使用数据库过滤器查询来查找用户,并设置适当的变量来确定模板中的操作。话虽如此,获得 forloop 计数器是可能的。在 for 循环内部,它由 forloop.counter (从 1 开始计数)或 forloop.counter0 (从 0 开始计数)给出。您可以在 Django 文档。

       {% 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 %} 

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 %} 
無處可尋 2024-11-11 11:38:15

您应该考虑改变您的重定向方式。在模板中迭代数据库中的所有用户并不是一个好主意,您应该编写一个过滤器或将逻辑移动到控制器文件中。装饰器将为您派上用场。对于您的问题,您无法在模板内设置计数器。如果您愿意,可以使用默认值
for 循环变量。我认为这对你没有任何用处。

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

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
满天都是小星星 2024-11-11 11:38:15

您应该在未经过身份验证的用户的视图上使用装饰器。尝试 @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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文