我需要存储一些来自某些非django应用程序的特殊cookie.我可以在视图中这样做
request.session[special_cookies'] = special_cookies
Run Code Online (Sandbox Code Playgroud)
但是在非视图py文件中,我需要访问这个特殊的cookie.
根据文档,我可以做到这一点
>>> from django.contrib.sessions.backends.db import SessionStore
>>> import datetime
>>> s = SessionStore(session_key='2b1189a188b44ad18c35e113ac6ceead')
>>> s['last_login'] = datetime.datetime(2005, 8, 20, 13, 35, 10)
>>> s['last_login']
datetime.datetime(2005, 8, 20, 13, 35, 0)
>>> s.save()
Run Code Online (Sandbox Code Playgroud)
如果我不提供会话密钥,Django将为我生成一个.我担心获得许多新会话密钥的效果.(当你有多个用户时我不认为这很好,对吧......?)
我希望用户将这个特殊的cookie绑定到用户的会话中.但是,出于安全原因,我不想将其保存在用户配置文件中.我们登录时会生成此cookie(我们的应用程序将发送此特殊cookie).我们希望在整个浏览会话中来回发送此cookie.
我该如何解决这个问题?
非常感谢你!
#views.py
request.session['special_cookies'] = library.get_special(user, pwd)
#library.py
def get_special_cookies(user, pwd):
res = get_special_cookies("http://foobar.com/api/get_special_cookies", user, pwd)
#foobar.py (also non-views)
def do_this(user, special_cookies)
Run Code Online (Sandbox Code Playgroud)
我很确定这很好......
#views_2.py
def dummy_views(request):
foobar.do_this(request.user, request.session['special_cookies'])
Run Code Online (Sandbox Code Playgroud)
但有些情况下,我不希望通过视图/调用get_sepcial_cookies获取我的特殊cookie.我希望它能持续下去.或者我是否在思考..?
这让我陷入困境(对不起,我对python仍然很新)感谢您提供任何帮助.
错误
Run Code Online (Sandbox Code Playgroud)print Student.MostFrequent() TypeError: unbound method必须使用Student实例作为第一个参数调用MostFrequent()(没有任何内容)
这个Student.MostFrequent()在最后(最后一行)被一直调用,def是该类中的最后一个def
编辑 - 命名约定
我的长码
import csv
class Student:
sports = []
ftopics = []
stopics = []
choice_list = []
choice_dict = {}
def __init__(self, row):
self.lname, self.fname, self.ID, self.gender, self.sport, self.movie, self.movieyr, self.country, self.ftopic, self.stopic = row
self.sports.append(self.sport)
self.ftopics.append(self.ftopic)
self.stopics.append(self.stopic)
def print_information(self):
return (self.lname, self.fname, self.ID, self.gender)
def print_first(self):
return (self.lname, self.fname, self.sport)
def print_second(self):
return (self.lname, self.fname, self.movie, self.movieyr)
def print_third(self):
return (self.lname, self.fname, self.country)
def print_fourth(self):
return (self.lname, …Run Code Online (Sandbox Code Playgroud) 我知道在HTML4.0中
<p>
Run Code Online (Sandbox Code Playgroud)
tag不是块级元素.在XHTML 1.0中怎么样?
谢谢
这是HTML4.0 http://www.w3.org/TR/html401/struct/text.html的参考
P元素代表一个段落.它不能包含块级元素(包括P本身).
那是我的误解吗?
from rauth import OAuth1Service
OAUTH_REQUEST = "https://bitbucket.org/!api/1.0/oauth/request_token"
OAUTH_AUTH = "https://bitbucket.org/!api/1.0/oauth/authenticate"
OAUTH_ACCESS = "https://bitbucket.org/!api/1.0/oauth/access_token"
service = OAuth1Service(
name='test',
consumer_key='xxxxxxxxxxxxxx',
consumer_secret='xxxxxxxxxxxxxxxxxxxx',
request_token_url=OAUTH_REQUEST,
access_token_url=OAUTH_ACCESS,
authorize_url=OAUTH_AUTH)
resp = service.get_raw_request_token()
print resp
Run Code Online (Sandbox Code Playgroud)
我继续使用Bitbucket并生成一个消费者密钥对,但回复是400.任何想法是怎么回事?
我看了Bitbucket文档,网址是正确的.
编辑
感谢@maxcountryman在这里抽出时间.
我刚看了他的linklin示例代码:
import os
from rauth import OAuth1Service
OAUTH_REQUEST = "https://bitbucket.org/!api/1.0/oauth/request_token"
OAUTH_AUTH = "https://bitbucket.org/!api/1.0/oauth/authenticate"
OAUTH_ACCESS = "https://bitbucket.org/!api/1.0/oauth/access_token"
service = OAuth1Service(
name='test',
consumer_key='blah',
consumer_secret='blah',
request_token_url=OAUTH_REQUEST,
access_token_url=OAUTH_ACCESS,
authorize_url=OAUTH_AUTH)
# You can run python -m SimpleHTTPServer if you want a local callback
rtoken, rtoken_secret = service.get_request_token(params={'oauth_callback': 'http://localhost:8000'})
authorize_url = service.get_authorize_url(rtoken)
print …Run Code Online (Sandbox Code Playgroud) 我正在阅读这个源代码:
https://github.com/leobalter/PimpMyCode/blob/master/index.html#L20
<script src="js/compiled.min.js?v=125" async></script>
Run Code Online (Sandbox Code Playgroud)
如果你摆脱整行,CodeMirror部分将会消失.但您可以将数字更改为您想要的任何内容,文本框将保留.
这个查询是?v=125做什么的?
我从未见过有人使用这个.
从一个月前开始学习Python,我发现这种语言简单实用.
我是一名Web开发人员,但我只有PHP经验,我使用过像CakePHP或Zend这样的框架.我熟悉最受欢迎的Django.但是,Django与Python 3不兼容.
有人知道支持Python 3的Python Web框架吗?我应该考虑使用纯Python,没有框架吗?如果是个好主意,我应该使用哪个网络服务器?Nginx和uwsgi或Gunicorn这样的反向代理,为什么?
I know how to handle optional arguments using getopts in Bash
#!/bin/bash
while getopts ":a:p:drh" opt; do
case "$opt" in
a) echo $OPTARG;;
esac
done
Run Code Online (Sandbox Code Playgroud)
But if my script expects ./script.sh arg1 [options], how do I tell getopts to skip arg1?
$ ./script.sh -a 1234
1234
$ ./script.sh arg1 -a 1234
$ ./script.sh arg1
$ ./script.sh -a 1234 arg1
1234
Run Code Online (Sandbox Code Playgroud)
As I see I can handle the argument if the argument is put in the last position. What "regex" …
我正在尝试执行以下操作
@shared_task
def task1():
return '1'
@shared_task
def task2(r1):
return '2'
@shared_task
def task3(r2):
return 'done'
@shared_task
def job():
chain = (task1.s() | task2.s() | task3.s() ).apply_async()
chain()
Run Code Online (Sandbox Code Playgroud)
但是我必须明确给出task2和task3论证,否则我会出错。
我该怎么做才能让结果流到下一个任务?
假设我们有
mystr = 'hello.py'
mystr = 'hello'
if not 'py' in mystr.rsplit('.')[1]:
Run Code Online (Sandbox Code Playgroud)
第一种情况很好,但第二种情况导致"超出范围"错误.
目标:
如果字符串中存在.py,则不执行任何操作.如果.py不是文件名的一部分,请执行XYZ.注意:用户输入可能有也可能没有.'extension-type'.
尝试:
检查长度,或将列表转换为字符串.
还有其他选择 我不确定这是否过于本地化.就是想.
谢谢.
我有一个像
@staticmethod
def add_transaction(name, date, amount, debit, user, category_id):
pass
Run Code Online (Sandbox Code Playgroud)
测试它们中的任何一个的最佳方法是None什么?
if not (name or date or amount or debit or user or category_id):
raise ValueError
Run Code Online (Sandbox Code Playgroud)