我從我的表單字段發送值,以便將它們存儲在django中的會話中。我正在做下面的事情,但它沒有在django中接收POST中的數據。jquery Ajax發佈更新視圖中的django sesions
<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吡嗪酰胺得到打印。它顯示一個空的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),
)
與視圖中的變化和通過limelights提到在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
好讀書,有我在我的urls.py WRT指定什麼ajax請求。我以爲它會使用URL中提到的URL在後...我不知道什麼是要寫在url.py與阿賈克斯調用...我讀了幾個例子,並沒有提到關於網址。 py – user2918313
那麼,你必須在urls.py文件中定義'/ update_session /'url,以便Django能夠把它提取出來。你可以請,張貼嗎? –
請查看更新的問題,爲我的應用程序使用urls.py。另外,達到預期結果的方式是否正確? – user2918313