在python中有一种方法可以将try/except转换为单行吗?
就像是...
b = 'some variable'
a = c | b #try statement goes here
Run Code Online (Sandbox Code Playgroud)
b声明的变量在哪里而c不是......所以c会抛出一个错误并且a会变成b......
这实际上只是一个"最佳实践"问题......
我发现在开发应用程序时,我经常会得到很多观点.
通常的做法是将这些视图分解为多个视图文件吗?换句话说......而不仅仅是拥有views.py,是否常见于views_1.py,views_2.py,views_3.py(但更合适,可能按类别命名)?
我有一个Django项目,我正在研究Pylinting.
我有几种情况,我希望能够递归地找到具有给定名称的所有文件和不同的pylint(使用不同的选项).例如,我想为pylinting urls.py和admin.py设置不同的选项
以下适用于1个目录..
pylint ./project_name/*/urls.py
Run Code Online (Sandbox Code Playgroud)
但是我想把它*递归...以便它钻进子目录.
有什么方法可以实现吗?
更新 我也希望它们都作为单个pylint输出运行,而不是顺序运行
我试图自动创建这样的东西:
<input type='text' name='asdf[]' />
<input type='text' name='asdf[]' />
<input type='text' name='asdf[]' />
Run Code Online (Sandbox Code Playgroud)
通过循环范围内的范围.我一直在尝试这样的事情,还有其他一些变化:
# in a model class
for i in range(1, prim+1):
self.fields['asdf'] = forms.CharField(label=i)
# in the template
<form action='#' method='post'>
{{form.as_p}}
</form>
Run Code Online (Sandbox Code Playgroud)
但我没有运气.
如何自动化输入数组?
**编辑**为了澄清,最终我需要能够访问模板中的字段,如下所示:
{% for input in form.fields.asdf %}
{{input}}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
那将有希望得到我上面显示的原始输入列表...
我正在调用如下表单,然后将其传递给模板:
f = UserProfileConfig(request)
Run Code Online (Sandbox Code Playgroud)
我需要能够访问表单中的request.session ...所以首先我尝试了这个:
class UserProfileConfig(forms.Form):
def __init__(self,request,*args,**kwargs):
super (UserProfileConfig,self).__init__(*args,**kwargs)
self.tester = request.session['some_var']
username = forms.CharField(label='Username',max_length=100,initial=self.tester)
Run Code Online (Sandbox Code Playgroud)
我收集的是,这不起作用,因为与设置用户名字段相比,构建表单时.
所以,接下来我尝试了这个:
class UserProfileConfig(forms.Form):
def __init__(self,request,*args,**kwargs):
super (UserProfileConfig,self).__init__(*args,**kwargs)
self.a_try = forms.CharField(label='Username',max_length=100,initial=request.session['some_var'])
username = self.a_try
Run Code Online (Sandbox Code Playgroud)
无济于事.
还有其他想法吗?
我有一个词典列表.我需要检查该列表中的所有字典是否都为空.我正在寻找一个简单的声明,将在一行中完成.
是否有单行方式来执行以下操作(不包括打印)?
l = [{},{},{}] # this list is generated elsewhere...
all_empty = True
for i in l:
if i:
all_empty = False
print all_empty
Run Code Online (Sandbox Code Playgroud)
python有些新东西...我不知道是否有一种速记内置方式来检查这个.提前致谢.
我有一些这样的模型设置:
class AppGroup(models.Model):
users = models.ManyToManyField(User)
class Notification(models.Model):
groups_to_notify = models.ManyToManyField(AppGroup)
Run Code Online (Sandbox Code Playgroud)
User对象来自django的身份验证系统.
现在,我正在尝试获取与当前用户所属的组有关的所有通知.我试过了..
notifications = Notification.objects.filter(groups_to_notify=AppGroup.objects.filter(users=request.user))
Run Code Online (Sandbox Code Playgroud)
但这给出了一个错误:
用作表达式的子查询返回的多行
我想这是因为groups_to_notify正在检查几个组.
如何根据用户所属的组获取针对用户的所有通知?
这篇文章与此相关: 在django admin中动态添加行内联
有没有办法在不使用javascript的情况下添加内联表单集?显然,会涉及页面刷新.
所以,如果表单有一个名为'add'的按钮...
我想我可以这样做:
if request.method=='POST':
if 'add' in request.POST:
PrimaryFunctionFormSet = inlineformset_factory(Position,Function,extra=1)
prims = PrimaryFunctionFormSet(request.POST)
Run Code Online (Sandbox Code Playgroud)
我认为每次添加1,然后使用发布数据填充表单.但是,似乎extra = 1不会为帖子数据添加1.
由于基于类的视图在Django中变得更好,我在实现基于类的视图时遇到了"最佳实践"问题.它基本上归结为URL模板标记.
给出这样的urls.py:
urlpatterns = patterns('some_app.views',
url(r'^$', 'index', name='some_app_index')
)
Run Code Online (Sandbox Code Playgroud)
该标记可以采用视图的路径:
{% url some_app.views.index %}
Run Code Online (Sandbox Code Playgroud)
或网址的名称:
{% url some_app_index %}
Run Code Online (Sandbox Code Playgroud)
现在,使用基于类的url conf,最终会得到一个这样的url:
from some_app.views import Index
urlpatterns = patterns('',
url(r'^$', Index.as_view(), name='some_app_index')
)
Run Code Online (Sandbox Code Playgroud)
这意味着使用{% url some_app.views.index %}不再有效但{% url some_app_index %}仍然有效.(而且{% url some_app.views.Index.as_view %}似乎不是一个解决方案).
所以,我的问题是,从模板中引用URL confs的最佳做法是什么?
到目前为止,我发现使用path.to.view方法更好,因为它是干净的命名空间.但是,基于类的视图看起来越来越好,使用url名称是一个更好的方法吗?在这种情况下,命名空间完全依赖于应用程序开发人员设置的名称属性,其方式是将网址名称与其他应用程序分开...
思考?我在Django文档中找不到"这样做"但如果有人写过这个,我很乐意阅读它.
Python 2.5,Django 1.2.1,最近的干草堆,最近的飞快移动
这是我第一次深入研究Django-Haystack.我正在关注Haystack的"入门"指南,一切似乎都很顺利,直到我去构建索引.
所以,运行"manage.py rebuild_index"会向我发回信息:
Traceback (most recent call last):
File "/Users/steenb/Documents/Aptana Studio Workspace/bucksac/buckshr/manage.py", line 11, in <module>
execute_manager(settings)
File "/Library/Python/2.5/site-packages/django/core/management/__init__.py", line 438, in execute_manager
utility.execute()
File "/Library/Python/2.5/site-packages/django/core/management/__init__.py", line 379, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Python/2.5/site-packages/django/core/management/base.py", line 191, in run_from_argv
self.execute(*args, **options.__dict__)
File "/Library/Python/2.5/site-packages/django/core/management/base.py", line 218, in execute
output = self.handle(*args, **options)
File "/Library/Python/2.5/site-packages/haystack/management/commands/rebuild_index.py", line 13, in handle
call_command('clear_index', **options)
File "/Library/Python/2.5/site-packages/django/core/management/__init__.py", line 166, in call_command
return klass.execute(*args, **defaults)
File "/Library/Python/2.5/site-packages/django/core/management/base.py", line 218, in execute
output = self.handle(*args, **options)
File …Run Code Online (Sandbox Code Playgroud) django ×8
python ×7
django-forms ×3
django-views ×3
django-urls ×1
exception ×1
pylint ×1
whoosh ×1