Nik*_*ntz 1 python django session google-app-engine redirect
我使用blobstoreuploadhandler,因此必须返回self.redirect,但我需要将值传递给我的模板.我该怎么做?如果我不能使用模板值,那么我想我可以使用会话变量,我已经包含了烧杯会话库,但我无法理解如何访问django模板中的会话变量.知道我应该怎么做吗?
我使用默认的内置django与谷歌应用程序引擎,我可以使用请求处理程序访问会话变量但我不明白如何在模板中执行它:
class Sessiontest(webapp.RequestHandler):
def get(self):
# Get the session object from the environ
self.session = self.request.environ['beaker.session']
# Check to see if a value is in the session
if 'counter' in self.session:
counter = self.session['counter'] + 1
self.session['counter'] = counter
else:
self.session['counter'] = 1
counter = 1
self.session.save()
self.response.out.write('counter: %d' % counter)
Run Code Online (Sandbox Code Playgroud)
谢谢
更新/编辑:我的问题几乎与使用Google App Engine(Webapp)的Django模板中的Accessing会话变量完全相同-但是使用库beaker而不是gaeutilities
更新:这是一些代码.我们看到使用HTTP GET传递值不会很好,因为有一个应该隐藏值的反垃圾邮件测试:
def post(self, view):
message = ''
challenge = self.request.get('recaptcha_challenge_field').encode('utf-8')
response = self.request.get('recaptcha_response_field').encode('utf-8')
remoteip = os.environ['REMOTE_ADDR']
cResponse = captcha.submit(
challenge,
response,
CAPTCHA_PRV_KEY,
remoteip)
if cResponse.is_valid:
isHuman=True
else:#failed anti-spam test and can try again
isHuman=False
#Reprint the form
import util
template_values = {'isHuman':isHuman,'user' : users.get_current_user(),}
template_values.update(dict(current_user=self.current_user, facebook_app_id=FACEBOOK_APP_ID))
template_values.update(dict(capture=captcha.displayhtml(public_key = CAPTCHA_PUB_KEY, use_ssl = False, error = None)))
path = os.path.join(os.path.dirname(__file__), 'market', 'market_insert.html')
self.redirect("/ai") # Here the values aren't passed on and I must make a redirect
Run Code Online (Sandbox Code Playgroud)
小智 5
如果您正在进行重定向,则可能需要使用要保留在GET字符串中的变量重定向.所以你重定向
/myview/
Run Code Online (Sandbox Code Playgroud)
至
/myview2/?variable1=value
Run Code Online (Sandbox Code Playgroud)
但是,我认为您应该真正了解为什么要进行重定向.我倾向于在POST到表单后执行它们,如果用户需要登录,我会重定向到登录屏幕
/authentication/login/?next=/view/they/wanted/to/see
Run Code Online (Sandbox Code Playgroud)
否则你可以把东西放在cookie中,但这不是最好的方法.
| 归档时间: |
|
| 查看次数: |
2113 次 |
| 最近记录: |