English 中文(简体)
Django, 会话器
原标题:Django, session attr

I am trying to use session to pass some data from one page to another page. Here is the code i wrote in ajax.py.

def save_cookie(request, query):
    request.session[ query ] = query

But when i call this dajaxice function.An error will occurred. As we all know that when we try to use dajaxice in html page, the error msg always is "sth goes wrong". I tried to debug save_cookie, but the mock request object i created has no session attr. However, if i do request.session="blah", it worked. If I directly use save_cookie(request,query). It will pop up the error msg that request object has no attr seesion...

代码是直截了当的,我没有看到任何错误,有人知道原因吗?

最佳回答

从未使用 Dajaxice / dajax 来帮助我。 只有几个点:

  • 您是否为届会提供支持( 并适当地与您联系)? < a href="https://docs.djangoproject.com/en/1.3/topics/http/sessions/"rel="nolfollow" >https://docs.djangoproject.com/en/1.3/topics/http/sessions/

  • 您可以使用记录模块(或简单的“打印”语句)追踪例外, 即:

    def save_cookie(request, query): try: request.session[ query ] = query except Exception, e: print e raise

打印语句的输出现在应该出现在您启动 Dev 服务器的 shell 中( 假设您与 Dev 服务器一起工作... 您与 Dev 服务器一起工作, 是不是? )

  • 仍然使用 dev 服务器, 您可以使用 pdb 切换到交互式调试 :

    def save_cookie(request, query): import pdb; pdb.set_trace() request.session[ query ] = query

然后尝试访问浏览器中的 URL, 切换到您的 shell, 然后在 pdb 会话中您可以检查请求并( 如果有的话) 请求. session objects etc.

NB:如果在阿帕奇或其他任何网络服务器后面运行,则不要这样做,只使用内建的dev服务器。

  • "request.session= blah " will create the "session" attribute on the "request" object if it doesn t exist (and possibly replace the real "session" object if it already existed), so it s neither a valid test nor something sensible to do

我的两分钱...

问题回答

我对Dajaxice一无所知

下列人员将研究模拟请求对象:

def save_cookie(request, query):
    if not hasattr(request,  session ):
        request.session = dict()
    request.session[ query ] = query




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签