我有一个生成qrcode的django表单,表单确实显示在浏览器上但是在提交后,它不会呈现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)
我没有在这里发布模板,但我认为他们没问题?知道为什么会发生这种情况......
:更新
这些是使用的两个模板..
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 %}
答案 0 :(得分:2)
那是因为你的视图没有返回
的结果render_to_response('qr_results.html', {'url': qc['url']})
在POST后进行重定向也是一种很好的做法(参见http://en.wikipedia.org/wiki/Post/Redirect/Get)。
答案 1 :(得分:0)
render_to_response('qr_results.html', {'url': qc['url']})
替换为
return render_to_response('qr_results.html', {'url': qc['url']})
忘记归还!!!现在就行了