jQuery Ajax发布更新视图中的django会话 [英] Jquery Ajax post to update django sessions in views

查看:85
本文介绍了jQuery Ajax发布更新视图中的django会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从表单字段发送值,以便将它们存储在django的会话中.我正在执行以下操作,但是在Django的POST中它没有接收到数据.

I am sending the values from my form fields , so as to store them in sessions in django. I am doing the following, but it does not receive the data in POST in django.

<script type="text/javascript">

    var eventID = $('#id_eventID').val();
    var start = $('#id_start').val();
   $('a').click(function () {   
    console.log("Inside click");    
    $.post({
       url: "/update_session/",
       type: "POST",
       data: {eventID: eventID, start: start},
       success: function(response){},
       complete: function(){},
       error: function (xhr, textStatus, thrownError){
        alert("Error in ajax post");
       }
    });
    });
</script>

 <a href="Event/{{field.name}}"> Add signal</a>

当我单击上面的链接时,我希望此脚本执行,这反过来又打开了一个新表单.因此,当我返回原始页面(填写表格后)时,我希望能够检索将存储在会话中的所有数据(以我上面的方式).

I want this script to execute when i click on the link above, which in turn opens a new form. So, when i come back to the original page (after filling out the form), i want to be able to retrieve all the data which will be stored in sessions (in the way i did above).

在我的 view.py 中,我有以下内容,

In my view.py i have the following,

def update_session(request):
    print request.POST
    if request.is_ajax():
  try:
    request.session['eventID'] = request.POST['eventID']
    request.session['start'] = request.POST['start']
  except KeyError:
    return HttpResponse('Error')
    else:
  raise Http404

有了这个,我的django termianl中什么也没印出来.它显示一个空的Querydict {}.

With this nothing get printed in my django termianl. It displays an empty Querydict{}.

此外,我实现所需功能的方法是否正确?还是有更好的方法来实现这一目标..我是Web开发的新手. 因此,某些来源或提示会很棒!

Also, is my approach of achieving the required functionality correct ? or is there a better way to achieve this..I'm a novice with web development.. So, some source or hints would be great!

更新: * urls.py *

from django.conf.urls import patterns, url
from EiEventService import views

urlpatterns = patterns('',
   url(r'^$', views.event_view),
   url(r'^create/$', views.event_create),
   url(r'^eventSignals/$', views.eventSignal_create),
   url(r'^Intervals/$', views.interval_create),
   url(r'^eventBaseLine/$', views.EventBaseline_create),
   #url(r'^(?P<event_id>.*)/$', views.editEvent),
)

随着视图的更改,并按照limlights的说明在urls.py中添加. 我收到以下错误.整个回溯是

With changes in the view and adding the in urls.py as mentioned by limelights. I am getting the following error. The entire traceback is,

Traceback (most recent call last):
File "/usr/lib/python2.7/wsgiref/handlers.py", line 86, in run
  self.finish_response()
File "/usr/lib/python2.7/wsgiref/handlers.py", line 127, in finish_response
  self.write(data)
File "/usr/lib/python2.7/wsgiref/handlers.py", line 210, in write
  self.send_headers()
File "/usr/lib/python2.7/wsgiref/handlers.py", line 268, in send_headers
  self.send_preamble()
File "/usr/lib/python2.7/wsgiref/handlers.py", line 192, in send_preamble
  'Date: %s\r\n' % format_date_time(time.time())
File "/usr/lib/python2.7/socket.py", line 324, in write
  self.flush()
File "/usr/lib/python2.7/socket.py", line 303, in flush
  self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 59495)
Traceback (most recent call last):
File "/usr/lib/python2.7/SocketServer.py", line 582, in process_request_thread
  self.finish_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 323, in finish_request
  self.RequestHandlerClass(request, client_address, self)
File "/usr/local/lib/python2.7/dist-packages/Django-1.5.1-py2.7.egg/django/core/servers/basehttp.py", line 150, in __init__
  super(WSGIRequestHandler, self).__init__(*args, **kwargs)
File "/usr/lib/python2.7/SocketServer.py", line 640, in __init__
  self.finish()
File "/usr/lib/python2.7/SocketServer.py", line 693, in finish
  self.wfile.flush()
File "/usr/lib/python2.7/socket.py", line 303, in flush
  self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe

推荐答案

您必须将/update_session/网址添加到urls.py中-这是您遇到的第一个也是最重要的问题.这也是您的脚本无法到达服务器的原因.

You have to add the /update_session/ url to your urls.py - this is the first and foremost problem you're having. This is also the reason for your script not reaching the server.

url(r'^/update_session/$', views.update_session)

将为此的网址格式.

此外,您在开发中还会遇到其他一些问题,因此,我绝对建议您通读

Also, You're having a few other issues that will arise later on in your development so I would definately recommend you reading through the tutorial here

这篇关于jQuery Ajax发布更新视图中的django会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆