我正在使用reportlab pdfgen来创建PDF.在PDF中有一个由...创建的图像drawImage.为此,我要么需要图像的URL,要么需要视图中图像的路径.我设法建立了URL但是如何获得图像的本地路径?
我如何获得URL:
prefix = 'https://' if request.is_secure() else 'http://'
image_url = prefix + request.get_host() + STATIC_URL + "images/logo_80.png"
Run Code Online (Sandbox Code Playgroud) 正如我所理解的那样,有两种方法可以做Python装饰器,既可以使用__call__类,也可以定义函数作为装饰器.这些方法的优点/缺点是什么?有一种首选方法吗?
例1
class dec1(object):
def __init__(self, f):
self.f = f
def __call__(self):
print "Decorating", self.f.__name__
self.f()
@dec1
def func1():
print "inside func1()"
func1()
# Decorating func1
# inside func1()
Run Code Online (Sandbox Code Playgroud)
例2
def dec2(f):
def new_f():
print "Decorating", f.__name__
f()
return new_f
@dec2
def func2():
print "inside func2()"
func2()
# Decorating func2
# inside func2()
Run Code Online (Sandbox Code Playgroud) 我正在用Tkinter编写幻灯片程序,但我不知道如何将背景颜色更改为黑色而不是标准浅灰色.如何才能做到这一点?
import os, sys
import Tkinter
import Image, ImageTk
import time
root = Tkinter.Tk()
w, h = root.winfo_screenwidth(), root.winfo_screenheight()
root.overrideredirect(1)
root.geometry("%dx%d+0+0" % (w, h))
root.focus_set()
root.bind("<Escape>", lambda e: e.widget.quit())
image = Image.open(image_path+f)
tkpi = ImageTk.PhotoImage(image)
label_image = Tkinter.Label(root, image=tkpi)
label_image.place(x=0,y=0,width=w,height=h)
root.mainloop(0)
Run Code Online (Sandbox Code Playgroud) 我有一个包含许多不同类的多个文件的文件夹.这些都可以在一个大文件中,但为了使它更容易阅读,我将它分成多个文件,具体取决于类的属性.
我想将文件夹中所有文件的所有类导入到__init__文件中,以便我可以从文件夹导入任何内容,而不知道它属于哪个文件.
例:
/kitchen
+ __init__.py
+ fridge.py
+ stove.py
+ cupboard.py
Run Code Online (Sandbox Code Playgroud)
现在我必须这样做
from kitchen.fridge import milk
Run Code Online (Sandbox Code Playgroud)
当我想做的时候
from kitchen import milk
Run Code Online (Sandbox Code Playgroud)
相当于我可以通过以下方式__init__.py完成:
from kitchen.fridge import *
from kitchen.stove import *
from kitchen.cupboard import *
Run Code Online (Sandbox Code Playgroud)
然后我就能做到
from kitchen import milk
Run Code Online (Sandbox Code Playgroud)
但是我希望它能够获取文件夹中的所有文件而不必明确指定它(这样文件就可以转储到那里然后使用).
有没有办法做到这一点?
我想根据变量是否存在来为变量分配不同的值,这可能吗?我的非工作示例可能会更清楚:
{% if username %}
{% with menu_user=username %}
{% elif recent_users %}
{% with sorted_users=recent_users|dictsortreversed:"timestamp" %}
{% with menu_user=sorted_users.0.username %}
{% endif %}
{% if menu_user %}
<div id="menu">
<ul>
<li><a href="/user/{{ menu_user|urlencode }}">Profile</a></li>
<li><a href="/user/{{ menu_user|urlencode }}/products/">Products</a></li>
</ul>
</div>
{% endif %}
{% if recent_users %}
{% endwith %}
{% endif %}
{% endwith %}
Run Code Online (Sandbox Code Playgroud)
我尝试做的伪代码:
if username:
menu_user = username
elif recent_users:
menu_user = sorted(recent_users)[0]['username']
if menu_user:
<div id="menu">
<ul>
<li><a href="/user/{{ menu_user|urlencode }}">Profile</a></li>
<li><a href="/user/{{ menu_user|urlencode …Run Code Online (Sandbox Code Playgroud) 有一个由装饰器包装的函数,它将函数的输出作为HTML返回.我想在没有装饰器的HTML包装的情况下调用该函数.这甚至可能吗?
例:
class a:
@HTMLwrapper
def returnStuff(input):
return awesome_dict
def callStuff():
# here I want to call returnStuff without the @HTMLwrapper,
# i just want the awesome dict.
Run Code Online (Sandbox Code Playgroud) 我有一个Web应用程序,存储有关最近访问过的页面的信息,让我们称之为A.当用户访问另一个页面类型,称为B时,我在顶部呈现一个菜单,其中有一个按钮,指向最近访问过的A页面.可以通过A页面或B页面访问B页面,并且用户可以将其视为A的属性.
这很好但是当有人打开两个标签并且在标签1中的B页面上有一个指向A页面A1的链接时,它们会打开一个新标签并访问A页面A2.当用户然后刷新选项卡1中的B页时,菜单变为A2.我希望能够识别正在使用的选项卡,以便我可以为选项卡1保存A1,以便它不会更改为A2.
使用一个标签时:
A1 -> B1 -> B2 (the menu points back to A1)
Run Code Online (Sandbox Code Playgroud)
使用两个标签时:
Time | T1 | T2 | T3
----------------+----------+----------+-------------
Tab 1: | A1 -> B1 | | [refresh B1]
Tab 2: | | A2 -> B3 |
----------------+----------+----------+-------------
Menu points to: | A1 A1 | A2 A2 | A2
Run Code Online (Sandbox Code Playgroud)
当用户返回选项卡1时,这会使用户在选项卡2(A2)中而不是A1中查看用户.
我担心无法获取浏览器标签ID,但有人可能有任何解决方法的想法?
我的模板中有一个字典,我希望以通常的方式循环
{% for key, value in dictionary.items %}
Run Code Online (Sandbox Code Playgroud)
但是在字典中我有一个调用的键'items',所以我的循环返回字典['items']的值并尝试将结果解压缩为键值.
如何告诉Django使用该功能items而不是访问密钥?
我开始有这么多的芹菜任务,我想将它们分解成较小的文件而不是将它们放在一个大的文件中tasks.py,但是我无法让芹菜去发现它们.
工作结构:
proj/
app/
tasks.py
Run Code Online (Sandbox Code Playgroud)
结构我想工作:
proj/
app/
tasks/
__init__.py
task1.py
task2.py
...
Run Code Online (Sandbox Code Playgroud)
但这里芹菜找不到我的任务.我尝试设置CELERY_IMPORTS,但后来我必须指定每个文件,并且文件太多了.如果我导入每个文件的内容为相同__init__.py.我希望自动发现所有任务,或至少任务目录中的文件中的所有任务.
我当然可以将每个任务目录中的所有文件列入CELERY_IMPORTS,但这看起来相当丑陋.
任何想法,如果这是可能的一个很好的方式?
所以我使用jQuery UI为单选按钮设置外观,但我不能让Django按照必须完成的方式呈现我的表单.
我需要这个结构:
<table>
<tr>
<td><label for="notify_new_friends">Notify when new friends join</label></td>
<td class="radio">
<input type="radio" name="notify_new_friends" id="notify_new_friends_immediately" value="1" checked="checked"/><label for="notify_new_friends_immediately">Immediately</label>
<input type="radio" name="notify_new_friends" id="notify_new_friends_never" value="0"/><label for="notify_new_friends_never">Never</label>
</td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
总而言之,我需要一个类(无线电)中的单选按钮,它们有一个input和一个label for.
当我在模板中渲染表单时,{{ profile_form.notify_new_friends }}我得到以下内容:
<ul>
<li><label for="id_notify_new_friends_0"><input type="radio" id="id_notify_new_friends_0" value="0" name="notify_new_friends" /> Immediately</label></li>
<li><label for="id_notify_new_friends_1"><input type="radio" id="id_notify_new_friends_1" value="1" name="notify_new_friends" /> Never</label></li>
</ul>
Run Code Online (Sandbox Code Playgroud)
除了list-part之外,这正是我想要的.所以我尝试循环它,这给了我不同格式的标签:
{% for item in profile_form.notify_new_friends %}
{{ item }}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
这给了我:
<label><input type="radio" name="notify_new_friends" value="0" …Run Code Online (Sandbox Code Playgroud) django ×6
python ×6
decorator ×2
browser-tab ×1
celery ×1
django-1.4 ×1
django-forms ×1
django-views ×1
javascript ×1
jquery ×1
python-2.7 ×1
tk-toolkit ×1
tkinter ×1