Django 1.5 と JQuery を使用して、ビューのフォームからデータを取得できません。
私のHTMLコードは次のとおりです。
<form method="post" class="form-inline" id="getemailbutton" onsubmit="getemail();return false">
<input type="text" id="email" class="input-small" placeholder="Email" />
<button name="submit" type="submit" class="btn" >Go!</button></form><!-- end of email form -->
私のJSコードは次のとおりです。
function getemail(){
div = 'getemailbutton';
ending="Thanks! We will contact you asap.";
console.log($('#email').val());
$('span#getemailbutton').css('display','none');
$('#getemailbutton').html(ending).fadeIn();
$.post("/getemail/",{ "email" : $('#email').val() });
}
ビューの私のコードは次のとおりです。
def getemail(request):
email = request.POST['email']
message = ("New lead by email:%s\n" % email)
Mail(message)
return HttpResponse(status=204)
私の urls.py は次のとおりです: Web インポート ビューから
urlpatterns = patterns('',
url(r'^$', TemplateView.as_view(template_name="index.html")),
url(r'^index/', TemplateView.as_view(template_name="index.html")),
url(r'^features/', TemplateView.as_view(template_name="features.html")),
url(r'^offer/', TemplateView.as_view(template_name="offer.html")),
url(r'^whatfor/', TemplateView.as_view(template_name="whatfor.html")),
url(r'^contact/', TemplateView.as_view(template_name="contact.html")),
url(r'^getemail/', views.getemail, name="getemail"),
url(r'^getphone/', views.getphone, name="getphone"),
エラーの始まりは次のとおりです。
MultiValueDictKeyError at /getemail/
"Key 'email' not found in <QueryDict: {}>"
Request Method: POST
Request URL: http://127.0.0.1:8000/getemail/
Django Version: 1.5.1
Python Executable: /usr/bin/python
Python Version: 2.7.3
Python Path: ['/home/chaica/progra/python/backupchecker', '/usr/local/lib/python2.7/dist-packages/django_blog_zinnia-0.13.dev-py2.7.egg', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.7']
Server time: lun, 28 Oct 2013 09:53:34 +0100
Installed Applications:
('web',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
115. response = callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/views/decorators/csrf.py" in wrapped_view
77. return view_func(*args, **kwargs)
File "/home/chaica/progra/python/backupchecker/web/views.py" in getemail
11. email = request.POST['email']
File "/usr/local/lib/python2.7/dist-packages/django/utils/datastructures.py" in __getitem__
295. raise MultiValueDictKeyError("Key %r not found in %r" % (key, self))
Exception Type: MultiValueDictKeyError at /getemail/
Exception Value: "Key 'email' not found in <QueryDict: {}>"
Request information:
GET: No GET data
POST: No POST data
FILES: No FILES data
COOKIES:
csrftoken = 'rfF4e8hzbSI77uszQd1LxFdodw02nfJa'
Context:
- CSRF はデバッグ目的で無効になっています。後で有効にします
- Firebug では、JS の console.log() に適切な値が表示されるため、HTML から JS スクリプトへのデータが機能することがわかります。
- Django、おそらく URL リダイレクトについて何かを疑っていますが、設定の何が問題なのかわかりません。