【问题标题】:Blank page due to "for template tag" in Django由于 Django 中的“模板标签”而导致的空白页面
【发布时间】:2021-04-05 15:34:27
【问题描述】:

在我在模板文件中添加 {% for %} 之前,我的网页运行良好。

这是我的 home.html 文件

<body>
<div>
    <div>
{% if not status %}
            <a href="/gmailAuthenticate" onclick="gmailAuthenticate()" title="Google">Google</a>
{% else %}
{% for item in contexto %}
<p>Your are verified</p>
            <form id="myform" method="post" action="{% url 'logout' id=item.id %}/?next={{request.path }}">
    {% csrf_token %}
    <input type="hidden" name="name" value="value" />
    <a onclick="document.getElementById('myform').submit();">disconnect</a>
</form>
{% endfor %}
{% endif %}
    </div>

</div>
</body>

这是我的主页功能

def home(request):
    status = True
    contexto = CredentialsModel.objects.all()

    if not request.user.is_authenticated:
        return HttpResponseRedirect('admin')

    storage = DjangoORMStorage(CredentialsModel, 'id', request.user, 'credential')
    credential = storage.get()
    try:
        access_token = credential.access_token
        resp, cont = Http().request("https://www.googleapis.com/auth/gmail.readonly",
                                    headers={'Host': 'www.googleapis.com',
                                            'Authorization': access_token})
    except:
        status = False
        print('Not Found')

    return render(request, 'index.html', {'status': status})

我检查了我的 errors.log 文件,但没有显示任何内容

【问题讨论】:

    标签: python html django for-loop django-views


    【解决方案1】:

    您没有向模板上下文提供 contexto 查询集:

    return render(request, 'index.html', {'status': status, 'contexto': contexto})
    

    【讨论】:

      【解决方案2】:

      您必须将变量 contexto 传递给您的模板。

      会是这样的:

      return render(request, 'index.html', {'status': status, 'contexto': contexto} )
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-04-25
        • 2010-10-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-10-14
        • 2012-01-02
        相关资源
        最近更新 更多