Django的会话持久性而丢失数据 [英] Django Session Persistent but Losing Data

查看:133
本文介绍了Django的会话持久性而丢失数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经工作了几个小时试图了解以下问题:我有一个用户发送一个Ajax请求动态发送的形式和记录形式来阅读提交的数量有所增加。为实现这一目标我用的request.session ['editing_foo'] = {'prefix_of_form_elements':p键} ,这样我可以将它们与数据库的保存和加载相关联(-1是那些尚未保存的新形式)。

I have been working for hours trying to understand the following problem: I have a user send an Ajax request to dynamically send a form and record that the number of forms to read on submission has increased. Toward this end I use request.session['editing_foo'] = { 'prefix_of_form_elements' : pkey } so that I can associate them with the database for saving and loading (-1 is for new forms that haven't been saved yet).

然而,当我用下面的code(参见下图)我得到以下奇怪的输出:

However, when I use the following code (see bottom) I get the following bizarre output:

1日点击:

{} foousername
next_key 1
1
{u'1-foo': -1}

2日点击:

2nd Click:

{} foousername
next_key 1
1
{u'1-foo': -1}

3请求​​:

3rd Request:

{} foousername
next_key 1
1
{u'1-foo': -1}

到底是什么回事?

What the heck is going on?

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))

谢谢大家了!

Thank you all very much!

注:这是一个随访到<一个href="http://stackoverflow.com/questions/1945182/django-development-server-not-serving-errors-from-ajax-requests">$p$pvious问题就同一源$ C ​​$ C。

Note: This is a followup to a previous question concerning the same source code.

推荐答案

我不认为我完全明白这个问题,但你可能想看看它<一个href="http://docs.djangoproject.com/en/dev/topics/http/sessions/#configuring-the-session-engine">session发动机你使用

I don't think I completely understand the question, but you may want to take a look at which session engine you're using

如果你使用,你需要确保你有适当的缓存设置缓存会议引擎(例如虚拟缓存只会扔出去会话数据)

if you're using the cache session engine you need to make sure you have caching properly set up (for instance the dummy cache would just throw out your session data)

另一种可能是您的会话没有被保存,因为你不改变会话,你改变存储在会话中的可变对象。你可以尝试迫使会话保存通过添加此放在你的观点:

another possibility is that your session isn't being saved because you're not changing the session, you're changing a mutable object that is stored in the session. you can try forcing the session to save by adding this somewhere in your view:

request.session.modified = True

这篇关于Django的会话持久性而丢失数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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