Jeril Jeril - 9 months ago 52
Python Question

Django - Template tags not working properly after form validation

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.

The following is my

class CompForm(forms.ModelForm):

class Meta:
model = Comp
fields = ('inp',)

The following is my

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)

The following is my index.html:

{% extends "comp/base.html" %}
{% load crispy_forms_tags %}

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

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

{% if errData %}
{{ errData.as_p }}
{% endif %}
{% endblock %}

In the terminal I am able to get the
in the dictionary, but its not getting displayed in the webpage. What might be the mistake? Kindly help.


You are trying to called the method as_p on strings which doesn't make sense.

as_p() is a helper method on form instances to make it easier to render them in the template so you need:

{{ form.as_p }}

you can also use as_table and as_ul

You can read more in the documentation