小编Ted*_*Ted的帖子

Django:从manage.py runserver命令覆盖Debug = True

有没有一种简单的方法可以告诉Django runserver覆盖settings.py文件中的单个变量?

我很乐意打电话给:

python manage.py runserver 0.0.0.0:8000 Debug=False
Run Code Online (Sandbox Code Playgroud)

有什么想法吗?

动机:有一个特定的站点,有数百个数据库查询来显示或保存特定的页面,我希望能够快速关闭调试,而无需编辑我的设置文件(有可能被遗忘).

django

14
推荐指数
2
解决办法
1万
查看次数

Python:如果三件事中有一件以上是真的,则返回false

我正在写一个django模型,允许我的网站有优惠券.

优惠券可以有三种类型:终身帐户凭证,特定月份凭证,一定数量的美元凭证.

为了简单起见,我只允许优惠券拥有三个可能值中的一个(即优惠券不能是10美元和5个月).但我想检查优惠券何时被保存以确保此规则为真.

目前我有:

true_count = 0
if self.months:
    true_count += 1
if self.dollars:
    true_count += 1
if self.lifetime:
    true_count += 1    

if true_count > 1:
    raise ValueError("Coupon can be valid for only one of: months, lifetime, or dollars")  
Run Code Online (Sandbox Code Playgroud)

我知道有更好的方法可以做到这一点,但我没有看到它(称之为编码器的块).

非常感谢帮助.

如果是maters,则三种类型是int,int和bool

months = models.IntegerField(default=0)
cents = models.IntegerField(default=0)
#dollars = models.FloatField(default=0.00)
#dollars replaced with integer cents per advice of group
lifetime = models.BooleanField(default=False)
Run Code Online (Sandbox Code Playgroud)

python django

9
推荐指数
2
解决办法
3766
查看次数

你如何麻烦谷歌分析代码?

任何人都可以分享谷歌anlytics代码疑难解答的最佳做法吗?

有人建立了调试工具吗?谷歌有隐藏的地方吗?有人有一个很好的分类逻辑图吗?

我会定期设置GA的不同部分,似乎每次我都需要4到5天才能使它工作.

工作流程如下所示:

Read the docs on the feature (e.g. events, custom variables).
Implement what appears to be the correct code based on the docs.
Wait a day.
See no data.
Google every version of the problem I can imagine.  Find what may be a solution.
Change my code.
Wait a day.
See no data.
Loop:
    Randomly move elements of the tracking code around.
    Wait a day.
    If other parts break, tell ceo, get yelled at, revert changes.
    If …
Run Code Online (Sandbox Code Playgroud)

javascript google-analytics

9
推荐指数
2
解决办法
1180
查看次数

Django非阻止电子邮件?缺少threading.thread或subprocess?

我有一个django网站.最终用户的某些操作会向组中的其他用户发送电子邮件.

当用户数量大于20时,它可以在请求周期中增加1-3秒,这是我不喜欢的.我希望能够从非阻止功能发送电子邮件.

我知道RabbitMQ和Celery可以解决这个问题,但有200个用户似乎过度工程,它增加了两个我必须安装,理解和保姆的应用程序.

我已经做了一些研究,看起来两个线程.线程和子进程都是包装非阻塞调用的方法.我错过了一个明显的方法吗?使用threading.thread或subprocess方法有缺点吗?

谢谢,特德

python django multithreading

5
推荐指数
1
解决办法
1074
查看次数

是否有一种优雅的方式来存储双重关系(即用户1和用户2是朋友)

我本月在两个不同的工作中遇到了同样的问题:

Version 1: User 1 & User 2 are friends
Version 2: Axis 1 & Axis 2 when graphed should have the quadrants colored...
Run Code Online (Sandbox Code Playgroud)

问题是,我没有看到使用RDBMS来存储和查询此信息的优雅方式.

有两种明显的方法:

方法1:

store the information twice (i.e. two db rows rows per relationship):
u1, u2, true 
u2, u1, true
u..n, u..i, true
u..i, u..n, true

have rules to always look for the inverse on updates: 
on read, no management needed
on create, create inverse
on delete, delete inverse
on update, update inverse

Advantage:    management logic …
Run Code Online (Sandbox Code Playgroud)

database database-design bit-manipulation bitmask social-networking

5
推荐指数
1
解决办法
382
查看次数

Django Querysets - 添加字符串文字注释

我想从字面上将字符串添加到查询集对象。为什么,因为我将它发送到 JSON,将信息放在那里并使其可用而无需遍历查询集将其转换为自定义字典,这将非常好和干净。

我现在所拥有的:

a_vote_set.aggregate(
                    count = Count('id'),
                    avg=Avg('score'),
                    std=StdDev('score'),
                    sum=Sum('score'),
                )
Run Code Online (Sandbox Code Playgroud)

这让我明白:

{"count": 1, "std": 0.0, "sum": -4.0, "avg": -4.0}
Run Code Online (Sandbox Code Playgroud)

我想得到的是:

{"count": 1, "std": 0.0, "sum": -4.0, "avg": -4.0, "additional_value": "name of candidate"}
Run Code Online (Sandbox Code Playgroud)

我很想通过调用这样的东西来获得它:

    a_vote_set.aggregate(
                        count = Count('id'),
                        avg=Avg('score'),
                        std=StdDev('score'),
                        sum=Sum('score'),
                        additional_value=Literal(candidate.name),
                    )

or this:

    a_vote_set.aggregate(
                        count = Count('id'),
                        avg=Avg('score'),
                        std=StdDev('score'),
                        sum=Sum('score')
                 ).append(
                        additional_value=str(candidate.name),
                 )
Run Code Online (Sandbox Code Playgroud)

关于这是否可能的任何想法?

django django-orm django-queryset

4
推荐指数
2
解决办法
8057
查看次数

从django模板调用python"DIR"函数,还是php print_r等效?

有没有人知道用于检查模板中元素的现有库?

目前我通常最终会在视图中执行此操作:

show_me = dir(the_deets_on_this_object)
raise ValueError()
Run Code Online (Sandbox Code Playgroud)

然后我可以在调试器堆栈跟踪中检查show_me的值.

但是,这真的很难看.我希望能够在模板中进行此检查,就像在php中使用print_r一样

{% load development_show_me %}
{{the_deets_on_this_object|show_them_to_me}}
Run Code Online (Sandbox Code Playgroud)

我知道这在django标准模板库中不存在,但我希望有人已经编写了这个模板过滤器,所以我可以愉快地使用它.

奖金行为:

  • 检查settings.DEBUG并引发错误,如果错误则不会被捕获
  • 将输出包装在<pre>中

如果必须,我会写它,但希望其他人已经这样做了.

django django-templates

4
推荐指数
2
解决办法
3492
查看次数

jQueryUI拖动:确保鼠标位于对象的中心

我正在构建一个图形界面,允许用户将外部点拖动到图形上.
然后我在drop上获得鼠标位置并在那里添加一个点到图表.

但是,点的直径为35px,因此可以在角落处抓住点.当发生这种情况时,在图表上绘制的点与用户期望点的位置明显不同.

我无法想象这是唯一一次出现这种情况,是否有一种简单的方法可以确保物体的中心点是药物?

jquery jquery-ui jquery-ui-draggable

2
推荐指数
1
解决办法
3749
查看次数