我正在从表单字段中发送值,以便将它们存储在 django 的会话中。我正在执行以下操作,但它没有在 django 的 POST 中接收数据。
<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>
我希望当我单击上面的链接时执行此脚本,这反过来会打开一个新表单。因此,当我回到原始页面(填写表格后)时,我希望能够检索将存储在会话中的所有数据(以我上面所做的方式)。
在我的view.py中,我有以下内容,
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{}。
另外,我实现所需功能的方法是否正确?还是有更好的方法来实现这一点..我是网络开发的新手..所以,一些来源或提示会很棒!
更新: * 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),
)
更改视图并添加 urls.py 中提到的聚光灯。我收到以下错误。整个追溯是,
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