Tôi đã làm việc hàng giờ để tìm hiểu vấn đề sau: Tôi có người dùng gửi yêu cầu Ajax để gửi biểu mẫu động và ghi lại số biểu mẫu để đọc khi gửi. Hướng tới mục đích này, tôi sử dụng request.session['editing_foo'] = { 'prefix_of_form_elements' : pkey }
để tôi có thể liên kết chúng với cơ sở dữ liệu để lưu và tải (-1 là dành cho các biểu mẫu mới chưa được lưu).Phiên Django liên tục nhưng mất dữ liệu
Tuy nhiên, khi tôi sử dụng đoạn mã sau (xem phía dưới) Tôi nhận được đầu ra kỳ lạ sau đây:
1st Bấm:
{} foousername
next_key 1
1
{u'1-foo': -1}
2 Bấm:
{} foousername
next_key 1
1
{u'1-foo': -1}
3 Yêu cầu:
{} foousername
next_key 1
1
{u'1-foo': -1}
Chuyện quái gì đang xảy ra?
id_fetcher = re.compile(r'\d')
@login_required
def ajax_add_foo(request):
def id_from_prefix(key):
return int(id_fetcher.search(key).group(0))
if 'editing_foos' not in request.session:
print "reinitializing"
request.session['editing_foos'] = {}
print request.session['editing_foos'], request.user
keys = request.session['editing_foos'].keys()
if len(keys) == 0:
next_key = 1
else:
print [ id_from_prefix(key) for key in keys ]
next_key = max([ id_from_prefix(key) for key in keys ]) + 1
print "next_key", next_key
fooform = FooForm(prefix=next_key)
print next_key
request.session['editing_foos'].update({create_prefix(FooForm, next_key) : -1 }) # This quote is new and has no pkey
print request.session['editing_foos']
return render_to_response('bar/foo_fragment.html',
{'fooform' : fooform, },
context_instance=RequestContext(request))
Cảm ơn tất cả các bạn rất nhiều!
Lưu ý: Đây là lần theo dõi previous question liên quan đến cùng một mã nguồn.
Đẹp! Bạn là một thiên tài! Tôi chưa bao giờ thấy đề cập đến tùy chọn đó trong tài liệu! – SapphireSun
Bạn tiết kiệm cuộc sống của tôi! Không thấy điều đó trong tài liệu –
Thanx bro ..! bạn tiết kiệm ngày của tôi: P –