Django18初体验


1408 浏览 5 years, 11 months

36 QUERYSET BASICS

版权声明: 转载请注明出处 http://www.codingsoho.com/

参考

views.py

def home(request):
    if request.user.is_authenticated() and request.user.is_staff:
        #print(SignUp.objects.all())
        # i = 1
        # for instance in SignUp.objects.all():
        #   print(i)
        #   print(instance.full_name)
        #   i += 1

        queryset = SignUp.objects.all().order_by('-timestamp') #.filter(full_name__iexact="Justin")
        #print(SignUp.objects.all().order_by('-timestamp').filter(full_name__iexact="Justin").count())
        context = {
            "queryset": queryset
        }

home.html

    {% if queryset %}
    <h2>Welcome Staff</h2>
    <table class='table'>
    {% for instance in queryset %}
        <tr>
            <td>{{ forloop.counter }}</td>
            <td>{{ instance.email }}</td>
            <td>{{ instance.full_name }}</td>
            <td>{{ instance.timestamp|timesince }} ago</td>
        </tr>
        {% if instance.email == "abc@gmail.edu" %}
        <tr><td>is equal</td></tr>
        {% endif %}
    {% endfor %}
    </table>
    {% endif %}