访问传递给django中模板的过滤器查询结果

时间:2015-06-13 17:01:50

标签: python django django-templates django-queryset

我想将过滤器查询结果传递给django中的模板。返回的每个字段的详细信息将以可读形式的形式呈现,并且类似于每行。我正在使用django 1.8

这是我的urls.py

url(r'^ pendingRegistrations / $',views.pendingApproval,name ='pendingApproval')

这是我的views.py

from django.shortcuts import render_to_response
from django.http import HttpResponse
from django.template.context_processors import csrf
from django.views.decorators.csrf import csrf_protect
from django.core.mail import send_mail
from django.core import mail
from fcui.models import *
from django.db.models import Q
import urllib
from django.template import Context

@csrf_protect
def pendingApproval(request):


        if request.method == 'POST' :

                return HttpResponse("Post done")

        elif request.method == 'GET' :

                data = request.GET.get('id')
                decode = urllib.unquote_plus(data)

                from encryptData import encryption, decryption
                groupId = decryption(decode)
                print "group id : " + groupId
        #else :

        #       userId = request.user_id
        #       query = account_manager_users.objects.get(user_id = userId)
        #       groupId = query.group_id

        print "group id : " + groupId
        pending_users_list = pending_user_registrations.objects.filter(group_id = groupId)
        print pending_users_list

        c = Context({'pending_users_list',pending_users_list})
        c.update(csrf(request))
        return render_to_response('fcui/pendingRegistrations.html',c)

这是我的.html(存储在模板文件夹中的app文件夹中)

{% block content %}

<h1 class="page-header">Pending User Registration Details</h1>


  {% if pending_users_list %}
        "cmn"
        {% for user_details in pending_users_list %}
                <form method="POST" action = "/appname/pendingRegistrations/">
                <table>
                <tr>
                <td>First Name : </td><td><input type = "text" name = "firstName" value = "{{user_details.first_name}}" readonly></input></t$
                </tr>
                <tr>
                <td>Last Name : </td><td><input type = "text" name = "lastName" value = "{{user_details.last_name}}" readonly></input></td>
                </tr>
                <tr>
                <td>User Name : </td><td><input type = "text" name = "userName" value = "{{user_details.user_name}}" readonly></input></td>
                </tr>
                <tr>
                <td>Email Address : </td><td><input type = "text" name = "emailAddress" value = "{{user_details.email_address}}" readonly></$
                </tr>
                <tr>
                <td>Joining Reason : </td><td><input type = "text" name = "reason" value = "{{user_details.joining_reason}}" readonly></inpu$
                </tr>
                </table>
                <br><br>
                <input type ="submit" name = "Submit" value= "Accept" ></input>
                <input type ="button" value= "Reject"></input><br>
                <input type = "hidden" name = "groupId" value = "{{user_details.group_id}}" readonly></input>
                {% csrf_token %}
                </form>

                <br><br><br>

        {% endfor %}
 {% else %}
        "not cmn"
 {% endif %}

但即使pending_users_list不为空,屏幕也会显示“ not cmn ”。为什么会这样?请帮忙。我是django的新手。

修改:

这是我在控制台上看到的:

[<pending_user_registraions: PRIYA GOEL Priya29 shubh@gmail.com invited>]

1 个答案:

答案 0 :(得分:0)

首先,看起来你在这一行上有错误:

c = Context({'pending_users_list',pending_users_list})

应该是:

c = Context({'pending_users_list': pending_users_list})

请注意,逗号更改为冒号。