Google App Engine で Django を使用すると、奇妙な問題が発生します。次のようなdjango-app内で定義されたファイルアップロードフォームがあります。
class ConvertForm(forms.Form):
from = forms.ChoiceField(choices=choices_from,
label='from:')
to = forms.ChoiceField(choices=choices_to,
label='to:')
class Meta:
fields = ('from','to')
そして、 app.views ファイルに次のものがあります。
def convert(request):
if request.POST:
form = ConvertForm(request.POST,request.FILES)
if form.is_valid():
from = form.cleaned_data['from']
to = form.cleaned_data['to']
# Redirect to a result page after post to avoid duplicates
return HttpResponseRedirect('/convert/results')
else:
form = ConvertForm()
args = {}
args.update(csrf(request))
args['form']=form
return render_to_response('convert.html',args)
私の convert.html テンプレートのフォーム部分は次のようになります。
<form action="/convert/convert/" method="post" enctype="multipart/form-data">{%\
csrf_token %}
<ul>
{{ form.as_ul }}
</ul>
<input type="submit" name="submit" value="Convert">
</form>
これはファイル アップロード フォーム (したがってマルチパート) であるはずですが、簡潔にするためにフォーム コンテンツを編集しました。
現在、適切な URL を参照しても何も起こりません。convert() 関数の本体を単純な関数に置き換えたため、正しい関数が呼び出されていることがはっきりとわかります
return render_to_response('convert_test.html',{'some_text':some_text})
some_text の値をブラウザ ウィンドウに表示します。GAE 内でフォームを処理するときに欠けている追加の癖はありますか、それともフォームで convert.html がレンダリングされないのはなぜですか? これはすべて localhost にあることに注意してください。まだデプロイしていません。
編集:これをいじった後、おそらくエラーの原因はテンプレートの継承にあるようです。convert.html ですべての django タグ {} を削除すると、フォームを正しくレンダリングできます。では、問題は、GAE 内でテンプレートの継承を適切に設定するにはどうすればよいかということです。
私の完全な convert.html テンプレートは次のようになります。
{% extends "base.html" %}
{% block content %}
<h2>[ convert ]</h2>
<form action="/convert/convert/" method="post" enctype="multipart/form-data">{% \
csrf_token %}
<ul>
{{ form.as_ul }}
</ul>
<input type="submit" name="submit" value="Convert">
</form>
{% endblock %}
base.html テンプレートの content ブロックを基本的に再定義します。これは、GAE でテストする前は完全に機能していたので、どういうわけか関与しているという感覚を揺るがすことはできません。
関連する場合、私の django settings.py はテンプレートに対して次のようになります。
TEMPLATE_DIRS = (
os.path.join(os.path.dirname(__file__),'..','convert','templates'),
os.path.join(os.path.dirname(__file__),'..','templates'),
)
前述したように、convert.html から {} タグを削除するとフォームが表示されますが、それ以外の場合は完全に白くて空のページに単独でレンダリングされます。
これは私のbase.htmlテンプレートの内容です
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/\
DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="description" content="Your description goes here" />
<meta name="keywords" content="your,keywords,goes,here" />
<meta name="author" content="Your Name" />
<link rel="stylesheet" type="text/css" href="/static/css/basic-minimal.css" ti\
tle="Basic Minimal" media="all" />
<title>Basic Minimal v1.2</title>
</head>
<body class="light">
<div id="wrap">
<div id="header">
<h1><a href="/convert/main">[ snazzy title ]</a></h1>
</div>
<div id="sidebar">
<ul>
<li><a href="/convert/convert">[ Convert ]</a></li>
<li><a href="/convert/transform">[ Transform ]</a></li>
<li><a href="/convert/help">[ Help ]</a></li>
<li><a href="/convert/references">[ References ]</a></li>
</ul>
</div>
<div id="content">
<h2>[ more title ]</h2>
<p>Text</p>
<p>More text</p>
</div>
</div>
</body>
</html>
これは「タイトル」ページでは (CSS が読み込まれる場合でも) 美しく機能しますが、これをベースとして上記の convert.html として他のテンプレートをレンダリングすることは機能しません。