Django - Template tags not working properly after form validation
<p>I am having a form which takes some value as input, and I am processing the input and returning the required output. Now when I tried to display the output it not displaying on the webpage. </p>

<p>The following is my forms.py:</p>

<pre><code>class CompForm(forms.ModelForm):

    class Meta:
        model = Comp
        fields = ('inp',)
</code></pre>

<p>The following is my views.py:</p>

<pre><code>def index(request):
    form = CompForm(request.POST or None)
    context = {
        'form': form,
    }
    print context
    if form.is_valid():
        ...
        ...
        outData = "The values you gave is correct"
        errData = "The values you gave is incorrect"
        print context
        context['outData'] = outData
        context['errData'] = errData
    print context

return render(request, 'comp/index.html', context)
</code></pre>

<p>The following is my index.html:</p>

<pre><code>{% extends "comp/base.html" %}
{% load crispy_forms_tags %}

{% block content %}
    &lt;div class="row"&gt;
        &lt;div class="col-md-8 col-md-offset-2"&gt;
            &lt;form method="post" action=""&gt;
                {% csrf_token %}
                {{ form|crispy }}
                &lt;input class="btn btn-primary" type="submit" name="Submit" /&gt;
            &lt;/form&gt;
        &lt;/div&gt;
    &lt;/div&gt;

    {% if outData %}
        {{ outData.as_p }}
    {% endif %}

    {% if errData %}
        {{ errData.as_p }}
    {% endif %}
{% endblock %}
</code></pre>

<p>In the terminal I am able to get the <code>outData</code> and <code>errData</code> in the dictionary, but its not getting displayed in the webpage. What might be the mistake? Kindly help.</p>

<p>You are trying to called the method <code>as_p</code> on strings which doesn't make sense. </p>

<p><a href="https://docs.djangoproject.com/en/1.10/topics/forms/#form-rendering-options" rel="nofollow"><code>as_p()</code></a> is a helper method on form instances to make it easier to render them in the template so you need:</p>

<pre><code>{{ form.as_p }}
</code></pre>

<p>you can also use <code>as_table</code> and <code>as_ul</code></p>

<p>You can read more in the <a href="https://docs.djangoproject.com/en/1.10/ref/forms/api/#ref-forms-api-outputting-html" rel="nofollow">documentation</a></p>

python django