krisdigitx
krisdigitx

Reputation: 7126

django form fails to render_to_response

i have a django form which generates the qrcode, the form does show up on the browser however after submitting, it does not render qr_results.html

views.py

from django.template.loader import get_template
from django.template import Template, Context
from django.http import HttpResponse, Http404
from django.shortcuts import render_to_response
from django.core.mail import send_mail
from django.http import HttpResponseRedirect
from cacoos_com.qr.qr_form import QrForm
from pygooglechart import QRChart

def qr(request):
    if request.method == 'POST':
        qform = QrForm(request.POST)
        if qform.is_valid():
            qc = qform.cleaned_data
            chart = QRChart(125,125)
            chart.add_data(qc['url'])
            chart.set_ec('H', 0)
            chart.download('/opt/cacoos_com/media/qr-hello.png')
            render_to_response('qr_results.html', {'url': qc['url']})
    else:
        qform = QrForm(initial={'url': 'type your url here'})
    return render_to_response('qr_form.html', {'form': qform})

qr_form.py

from django import forms

class QrForm(forms.Form):
    url = forms.CharField(max_length=100)

i have not posted the templates here but i think they ok? any idea why this could be happening....

:update

these are the two templates used..

qr_form.py

{% extends "base.html" %}
<html>
<head>
    <title>QR generator</title>
</head>
<body>
{% block content %}
    <h3>QR encoder</h3>

    {% if form.errors %}
        <p style="color: red;">
            Please Correct the error{{ form.errors|pluralize }} below.
        </p>
    {% endif %}

    <form action="" method="POST">
        <div class="field">
            {{ form.url.errors }}
            <label for="id_url">Url:</label>
            {{ form.url }}
        </div>
        <input type="submit" value="Submit">
    </form>

{% endblock %}
</body>
</html>

qr_results.html

{% extends "base.html" %}

{% block content %}
<h3>QR code</h3>

<p> This is the qr code for your url {{ url }} </p>

<p><img src="{{ MEDIA_URL}}/media/qr-hello.png" /"></p>

{% endblock %}

Upvotes: 1

Views: 2168

Answers (2)

krisdigitx
krisdigitx

Reputation: 7126

render_to_response('qr_results.html', {'url': qc['url']})

replaced to

return render_to_response('qr_results.html', {'url': qc['url']})

forgot to return it!!!works now

Upvotes: 0

Mikhail Korobov
Mikhail Korobov

Reputation: 22238

That's because your view doesn't return the result of

render_to_response('qr_results.html', {'url': qc['url']})

It is also a good practice to do a redirect after POST (see http://en.wikipedia.org/wiki/Post/Redirect/Get ).

Upvotes: 2

Related Questions