Django 1.5丢失了表单中的数据

时间:2013-10-28 09:03:30

标签: jquery django

使用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如下:     来自网页导入视图

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的事情,可能是网址重定向,但我找不到我的设置有什么问题。

1 个答案:

答案 0 :(得分:1)

这是你的问题:你在开始时调用console.log,这样你就可以看到$("#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 });
}