通常,我使用dispatch基于类的视图的方法来设置一些初始变量或根据用户的权限添加一些逻辑.
例如,
from django.views.generic import FormView
from braces.views import LoginRequiredMixin
class GenerateReportView(LoginRequiredMixin, FormView):
template_name = 'reporting/reporting_form.html'
form_class = ReportForm
def get_form(self, form_class):
form = form_class(**self.get_form_kwargs())
if not self.request.user.is_superuser:
form.fields['report_type'].choices = [
choice for choice in form.fields['report_type'].choices
if choice[0] != INVOICE_REPORT
]
return form
Run Code Online (Sandbox Code Playgroud)
它按预期工作:当匿名用户访问页面时,调用LoginRequiredMixin的dispatch方法,然后将用户重定向到登录页面.
但是,如果我想为此视图添加一些权限或设置一些初始变量,例如,
class GenerateReportView(LoginRequiredMixin, FormView):
def dispatch(self, *args, **kwargs):
if not (
self.request.user.is_superuser or
self.request.user.is_manager
):
raise Http404
return super(GenerateReportView, self).dispatch(*args, **kwargs)
Run Code Online (Sandbox Code Playgroud)
在某些情况下它不起作用,因为dispatch视图继承的mixins的方法尚未被调用.因此,例如,为了能够请求用户的权限,我必须重复以下验证LoginRequiredMixin:
class GenerateReportView(LoginRequiredMixin, FormView):
def …Run Code Online (Sandbox Code Playgroud) 在 Postgres 9.5 中:
select to_char(111, 'FM9,999'); -- gives 111
select to_char(1111, 'FM9,999'); -- gives 1,111
select to_char(11111, 'FM9,999'); -- gives #,### !!!
Run Code Online (Sandbox Code Playgroud)
如何在没有预先了解/假设最大可能数字数的情况下用逗号格式化我的数字,最好使用上面的内置内容?
我们在javascript中遇到了Math.round()的问题.问题是此函数不能正确舍入负数.例如 :
1.5~ = 2
0.5~ = 1
-0.5~ = 0 //错了
-1.5~ = -1 //错了
根据算术舍入,这是不正确的.-0.5的正确数字应为-1,-1.5应为-2.
有没有标准的方法,在Javascript中正确舍入负数?
在Django中显示表单非常简单:
<form action="" method="post">{% csrf_token %}
{{ form }}
<input type="submit" value="Update" />
</form>
Run Code Online (Sandbox Code Playgroud)
它基本上只是一个字-显示{{ form }}。非常简单,您可以将同一模板用于不同的表单。
fields = []如果您使用的是CBV,例如CreateView或UpdateView,则可以使用列表限制显示在表单上的字段。
与此并行的是,人们希望也有类似的工作流来显示模型(与编辑相反),例如在DetailView中。但是,没有这种事情。您必须为使用的每个DetailView编写一个自定义模板。如:
<h3>User: {{ user }}</h3>
<label>First Name</label>: {{ user.first_name }} <br />
<label>Last Name</label>: {{ user.last_name }} <br />
<label>Username</label>: {{ user.username }} <br />
<label>School</label>: {{ user.person.school.name }} <br />
Run Code Online (Sandbox Code Playgroud)
{{ form }}除了在此处打印的字段值之外,这与生成的内容非常相似input。
因此,我想知道为什么没有DetailView的可重用通用模板?对此有技术限制吗?还是它不像我想象的那样可重用?
Django dumpdata(未指定应用程序)将所有已安装应用程序的所有表转储到输出文件中。我刚刚意识到这不包括 django_migrations 表。我检查了其他 django 表,它们被包含在 INSTALLED_APPS 设置中,如下所示:
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
Run Code Online (Sandbox Code Playgroud)
现在,我很好奇,为什么不转储 django_migrations 表。基本原理似乎是当您创建新数据库并迁移时,它将自动生成并填充数据。这样对吗?如果是这样,我的第二个问题是有没有办法将它也转储(作为备份)?我是 Django 的新手,尝试新事物可能会打破这张桌子。有备份会很方便。
Django文档说,我们可以在压缩迁移后删除它们:
您应该进行此迁移,但将旧迁移保留在原位置;新的迁移将用于新安装。一旦确定代码库的所有实例都应用了压缩的迁移,就可以删除它们。
在这里,删除是否意味着仅删除迁移文件或django_migrations表中的条目?
这里是一些背景:我只有开发机器,所以只有一个代码库。压缩一些已经应用的迁移后,我删除了文件和数据库条目。通过进行迁移测试是否可以,它没有找到任何东西。因此,一切看起来都不错。第二天,我不得不进行一些更改,并进行了迁移。当我尝试迁移时,它也尝试应用压缩的迁移(在压缩之前已部分应用)。因此,我不得不返回并在django_migrations表中重新创建条目。因此,似乎我不得不保留数据库条目。我试图确保在再次弄乱任何东西之前,先弄清楚为什么它看起来不错,然后再尝试应用压缩后的迁移。
我有以下类型的几个大查询(为了清楚起见简化了)。
create function myfunction()
returns void
as $$
begin
...
with t as (
total as total,
total * 100 / total as total_percent,
total / people.count as total_per_person,
part1 as part1,
part1 * 100 / total as part1_percent,
part1 / people.count as part1_per_person,
part2 as part2,
part2 * 100 / total as part2_percent,
part2 / people.count as part2_per_person,
...
from (
select
total.amount as total
part1.amount as part1
part2.amount as part2
...
people.amount as people
from (select ...from mytable..) …Run Code Online (Sandbox Code Playgroud) sql postgresql plpgsql common-table-expression postgresql-9.3
django ×4
postgresql ×2
django-views ×1
javascript ×1
math ×1
migration ×1
mixins ×1
plpgsql ×1
rounding ×1
sql ×1
squash ×1