在Python中,如何在不调用函数的情况下将函数名称作为字符串获取?
def my_function():
pass
print get_function_name_as_string(my_function) # my_function is not in quotes
Run Code Online (Sandbox Code Playgroud)
应该输出"my_function".
这样的功能在Python中可用吗?如果没有,get_function_name_as_string在Python中如何实现任何想法?
在Django表单中,如何将字段设置为只读(或禁用)?
当表单用于创建新条目时,应启用所有字段 - 但是当记录处于更新模式时,某些字段必须是只读的.
例如,在创建新Item模型时,所有字段都必须是可编辑的,但在更新记录时,有没有办法禁用该sku字段以使其可见,但无法编辑?
class Item(models.Model):
sku = models.CharField(max_length=50)
description = models.CharField(max_length=200)
added_by = models.ForeignKey(User)
class ItemForm(ModelForm):
class Meta:
model = Item
exclude = ('added_by')
def new_item_view(request):
if request.method == 'POST':
form = ItemForm(request.POST)
# Validate and save
else:
form = ItemForm()
# Render the view
Run Code Online (Sandbox Code Playgroud)
班级ItemForm可以重复使用吗?ItemForm或者Item模型类需要进行哪些更改?我是否需要编写另一个类" ItemUpdateForm"来更新项目?
def update_item_view(request):
if request.method == 'POST':
form = ItemUpdateForm(request.POST)
# Validate and save
else:
form = ItemUpdateForm()
Run Code Online (Sandbox Code Playgroud) 在Python中,如何检查字符串是否只包含某些字符?
我需要检查一个只包含a..z,0..9和的字符串.(期间),没有其他性格.
我可以迭代每个字符并检查字符是a ..z或0..9,或.但那会很慢.
我现在还不清楚如何使用正则表达式来完成它.
它是否正确?你能建议一个更简单的正则表达式或更有效的方法吗?
#Valid chars . a-z 0-9
def check(test_str):
import re
#http://docs.python.org/library/re.html
#re.search returns None if no position in the string matches the pattern
#pattern to search for any character other then . a-z 0-9
pattern = r'[^\.a-z0-9]'
if re.search(pattern, test_str):
#Character other then . a-z 0-9 was found
print 'Invalid : %r' % (test_str,)
else:
#No character other then . a-z 0-9 was found
print 'Valid : %r' % (test_str,)
check(test_str='abcde.1')
check(test_str='abcde.1#')
check(test_str='ABCDE.12')
check(test_str='_-/>"!@#12345abcde<')
''' …Run Code Online (Sandbox Code Playgroud) 我知道有关实体组如何在GAE存储中工作的所有细节,但昨天(在Palo Alto的App Engine聚会上),作为主持人解释他对实体组的使用,让我感到震惊的是我从未真正使用它们在我自己的GAE应用程序中,我不记得在我使用的开源GAE应用程序中看到它们.
因此,我怀疑我只是忽略了(没有注意到或记住)这样的例子,因为我根本不习惯他们足以立即将"使用实体组"连接到"正在解决的那种应用程序问题" - 而我我认为我应该通过研究这个目标来解决这个问题,重点关注EG使用的问题(即,为什么应用程序与它一起工作,但如果没有它就无法工作或不能正常工作).
任何人都可以建议这些代码的良好URL吗?(随笔也将受到欢迎,如果他们专注于应用级解决问题,但没有如果,最喜欢我所看到的,他们只是专注于EG的是如何工作的细节- !).
是一个开源/商业工具/框架,可用于Windows XP/Vista上的谷歌Chrome浏览器中的自动化Web应用程序测试吗?(alpha/beta工具也行)
谢谢
如何在Django中根据域名或TLD设置urlpatter?
对于某些链接,亚马逊根据其网站tld以母语显示网址.
http://www.amazon.de/bücher-buch-literatur/(de:books =>bücher)
http://www.amazon.fr/Nouveautés-paraître-Livres/(fr:books => Livres)
http://www.amazon.co.jp/和书 - ユーズドブッ - 英语学习/ (jp:books =>和书)
(链接不完整,只显示为样本.)
是否可以在urls.py中获取主机名?(请求对象在urls.py中不可用)或者可能在process_request中间件中并在urls.py中使用它(如何???)
任何替代建议如何实现这一点?
#---------- pseudocode ----------
website_tld = get_host(request).split(".")[-1]
#.fr French : Books : Livres
#.de German : Books : Bücher
if website_tld == "fr":
lang_word = "Livres"
elif website_tld == "de":
lang_word = "Bücher"
else:
lang_word = "books"
urlpatterns = patterns('',
url(r'^%s/$' % lang_word,books_view, name="books"),
)
Run Code Online (Sandbox Code Playgroud)
url模式需要基于tld构建,然后在模板中构建,<a href="{% url books %}" >{% trans "books" %}</a>以将html呈现为<a href="Bücher">Bücher</a>或<a href="Livres">Livres</a>
什么是技术/设计/非SEO WWW或不带www的利弊,对于域以及子域?
来自Jeff Atwood的推文http://twitter.com/codinghorror/status/1637428313:
"有点后悔no-www选择,因为它会导致所有子域名都提交完整的cookie.:("
这是什么意思?有博客吗?帖子或文章详细说明了这个?
www应该考虑哪些其他具体问题及其原因.vs no-www.
更新:
在搜索有关此主题的更多信息时,我发现以下有用(除了Laurence Gonsalves答案):
对于继承块的 2 个子模板文件,{{ block.super }}未解析
Python 2.5.2、Django 1.0、Windows XP SP3
所涉及文件的示例框架代码:
base.htmlitem_base.htmlshow_info_for_all_items.htmlshow_info_for_single_item.html文件 : base.html
{% block content %}
{% endblock %}
Run Code Online (Sandbox Code Playgroud)
文件 : item_base.html
{% extends "base.html" %}
{% block item_info %}
Item : {{ item.name }}<br/>
Price : {{ item.price }}<br/>
{% endblock %}
Run Code Online (Sandbox Code Playgroud)
文件 : show_info_for_all_items.html
{% extends "item_base.html" %}
{% block content %}
<h1>info on all items</h1>
<hr/>
{% for item in items %}
{% block item_info %}
{{ block.super }} …Run Code Online (Sandbox Code Playgroud) 当网页移动到新位置时,如何显示移动的网页并在Django中返回301永久重定向HTTP响应状态代码?