如果在django模板中

时间:2014-10-25 14:43:45

标签: python-3.x django-templates

我的if / else条件进入模板时遇到问题。 我有一个简单的表格和确认按钮。用户应该以这种形式输入文本,然后按下按钮,只有在此之后,用户才能看到页面的其他部分。

这是我的模板代码:

{% extends "home/base.html" %}
{% load staticfiles %}
{{% block body %}

<h1 align="center">Analyzed text</h1>
    <form class="form" action="analyze" method="post">
        <div id="form">
            <dd><textarea id="textarea" name="q" rows=10 cols=55 placeholder=" Input your text in japanese here ..."></textarea>
            <dd><input class="myButton" type="submit" value="Analyze">
        </div>
    </form>
<div class=page>
    {% if 'q' != None %}
        {% include "kanji_analyzer/show_results.html" %}
    {% endif %}

</div>
{% endblock %}}

views.py

from django.shortcuts import render_to_response
from kanji_analyzer.logic_part.kanji_analyzer import KanjiesText


def start_kanji(request):
    return render_to_response('kanji_analyzer/main.html')


def show_entries():
    global analyzed_text
    global list_of_percents
    global list_of_find_kanjies
    return render_to_response('show_entries.html', {'analyzed_text': analyzed_text, 'list_of_percents': list_of_percents,
                                                 'list_of_find_kanjies': list_of_find_kanjies,})


def send_text(request):
    if 'q' in request.POST:
        q = request.POST['q']
        a = KanjiesText(request.POST['q'])
        a.remove_spaces_from_text()
        list_of_percents = a.take_percent_count()
        list_of_find_kanjies = a.list_find_kanjies()
        for i in range(0,5):
            if len(list_of_find_kanjies[i]) == 0:
                list_of_find_kanjies[i] = ''
        return render_to_response('kanji_analyzer/main.html', {'q': q, 'list_of_percents': list_of_percents,
                                                 'list_of_find_kanjies': list_of_find_kanjies,})
    else:
        q = "your form is empty"
        return render_to_response('kanji_analyzer/main.html', {'q': q},)

此块如果/ else不起作用。 'q' - 用户文字的值

0 个答案:

没有答案