如果我在负载均衡器后面设置多个django服务器,我是否希望SECRET_KEY相同,不同或者根本不重要?文档在这个值的确切含义上有点薄.
我正在使用django 1.4并尝试将本文末尾描述的代码转换为自定义标签.这意味着我需要访问请求中的is_secure和site_name值.这是settings.py中的我的CONTEXT_PROCESSORS:
CONTEXT_PROCESSORS = (
'django.core.context_processors.request',
'django.contrib.auth.context_processors.auth',
)
Run Code Online (Sandbox Code Playgroud)
这是我的模板标记代码:
from django import template
register = template.Library()
@register.simple_tag(takes_context=True)
def full_static_url(context, url):
request = context['request']
scheme = 'http'
if request.is_secure:
scheme += 's'
return scheme + '://' + request.site_name + context['STATIC_URL'] + url
Run Code Online (Sandbox Code Playgroud)
在我的视图代码中,我使用了新的渲染快捷方式,如下所示:
return render(request, 'myapp/mytemplate.html', {'foo':bar})
Run Code Online (Sandbox Code Playgroud)
我在模板中这样称呼它:
{% full_static_url "images/logo.gif" %}
Run Code Online (Sandbox Code Playgroud)
问题是,当它到达行request = context ['request']时,它会抛出一个KeyError,因为'request'不在上下文中.
我在这做错了什么?
完整的追溯是:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "C:\Projects\blah\blah\myapp\views\myview.py" in manifestcosts
44. return render(request, 'myapp/mytemplate.html', {'foo':bar}) …Run Code Online (Sandbox Code Playgroud) 这似乎应该是一个已经解决的经典发票项目问题,但也许我在搜索中没有使用正确的词。
我正在运行这样的查询(这只是一个简单的例子,我的实际查询要复杂得多,但它返回相同的结果):
select invoice.inv_num, item.name, item.qty
from invoice invoice, item
where invoice.inv_num = item.inv_num
order by invoice.inv_num
Run Code Online (Sandbox Code Playgroud)
我需要生成一个项目编号列,该列为每个项目递增,但每个新发票编号从 1 开始。因此,例如,我需要最终结果如下所示:
inv_num item_num name qty
------- -------- ------------- ---
111 1 red widgets 10
111 2 blue widgets 5
222 1 green_widgets 7
222 2 red_widgets 16
222 3 black_widgets 10
333 1 blue_widgets 8
333 2 red_widgets 12
Run Code Online (Sandbox Code Playgroud)
我们仍在使用 Oracle 9i 以防万一。
我正在尝试使用ProcessPoolExecutor,但是出现错误“队列对象仅应通过继承在进程之间共享”,但我没有使用队列(至少没有明确地使用)。我找不到任何可以解释我做错事情的信息。
这是一些演示问题的代码(不是我的实际代码):
from concurrent.futures import ProcessPoolExecutor, as_completed
class WhyDoesntThisWork:
def __init__(self):
self.executor = ProcessPoolExecutor(4)
def execute_something(self, starting_letter):
futures = [self.executor.submit(self.something, starting_letter, d) for d in range(4)]
letter = None
for future in as_completed(futures):
letter = future.result()
print(letter)
def something(self, letter, d):
# do something pointless for the example
for x in range(d):
letter = chr(ord(letter) + 1)
if __name__ == '__main__':
WhyDoesntThisWork(). execute_something('A')
Run Code Online (Sandbox Code Playgroud)
El Ruso指出,使something()成为静态方法或类方法会使错误消失。不幸的是,我的实际代码需要使用self调用其他方法。