0

使用 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 如下: from web import views

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,我看到从 HTML 到 JS 脚本的数据有效,因为好的值出现在 JS 的 console.log() 中。
  • 我怀疑有关 Django 的一些问题,可能是 url 重定向,但我找不到我的设置有什么问题。
4

1 回答 1

1

这是您的问题:您console.log在开始时调用,因此您可以真正看到它的存在,但是然后您通过调用 that 来$("#email").val()删除输入。所以最后,何时被调用,不再存在。将该值保存到某个变量中,以后可以使用:email$('#getemailbutton').html(ending)$.post()$("#email")

function getemail(){
    div = 'getemailbutton';
    ending="Thanks! We will contact you asap.";
    console.log($('#email').val());
    var email = $('#email').val()'
    $('span#getemailbutton').css('display','none');
    $('#getemailbutton').html(ending).fadeIn();
    $.post("/getemail/",{ "email" : email });
}
于 2013-10-28T10:35:55.987 回答