小编Seá*_*yes的帖子

如何在Django模板中访问枚举类型

我遇到了一个问题,无论我做什么,我都无法访问我的Django模板中的IntEnum(来自enum34 lib).

通过将其转换为dict,我能够绕过它:

def get_context_data(self, **kwargs):
    context = super(MyView, self).get_context_data(**kwargs)
    # Django templates don't play nice with Enums
    context['DEMOS'] = {d.name: d for d in DEMOS}
    # `context['DEMOS'] = DEMOS` doesn't work
    return context
Run Code Online (Sandbox Code Playgroud)

当DEMO是IntEnum时这些不起作用,但是当DEMO转换为dict时这样做:

{{ DEMO.FOO }}  # outputs nothing
{{ DEMO.FOO|default_if_none:'foo' }}  # outputs nothing
{{ DEMO.FOO.value }}  # outputs nothing
{% if DEMO.FOO == 1 %}  # no matter what I compare to, always False
Run Code Online (Sandbox Code Playgroud)

有什么想法吗?这是一个已知的问题?

python django enums django-templates

11
推荐指数
2
解决办法
4276
查看次数

如何使用guncorn进行Django日志记录

我有一个Django 1.6站点运行gunicorn,由主管管理.在测试和runserver期间,我登录了控制台,但是使用gunicorn,语句不会显示在任何地方(甚至不是ERROR级别日志).它们应该在/var/log/supervisor/foo-stderr---supervisor-51QcIl.log中,但它们不是.我使用supervisor在不同的机器上运行芹菜,其调试语句在其管理程序错误文件中显示正常.

编辑:在前台运行gunicorn表明我的错误消息没有像运行manage.py时那样记录到stderr.这绝对是一个枪手问题而不是主管问题.

django logging supervisord gunicorn

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

8
推荐指数
1
解决办法
1655
查看次数

加载Django fixture时出现问题:IntegrityError:(1062,"关键'user_id'的重复条目'4'"

我使用以下命令生成2个灯具:

./manage.py dumpdata --format=json --indent=4 --natural auth.User > fixtures/user.json
./manage.py dumpdata --format=json --indent=4 --natural --exclude=contenttypes --exclude=auth > fixtures/full.json
Run Code Online (Sandbox Code Playgroud)

我有以下名为user.json的夹具:

[
    {
        "pk": 4, 
        "model": "auth.user", 
        "fields": {
            "username": "foo", 
            "first_name": "Se\u00e1n", 
            "last_name": "Hayes", 
            "is_active": true, 
            "is_superuser": true, 
            "is_staff": true, 
            "last_login": "2010-09-27 21:57:45", 
            "groups": [], 
            "user_permissions": [], 
            "password": "!", 
            "email": "foo@gmail.com", 
            "date_joined": "2010-09-27 21:57:45"
        }
    }
]
Run Code Online (Sandbox Code Playgroud)

以及名为full.json的以下夹具:

[
    {
        "pk": "72a75887b4a0ce06a61f9183fe1c0e15", 
        "model": "sessions.session", 
        "fields": {
            "expire_date": "2010-10-11 21:57:45", 
            "session_data": "gAJ9cQEoVRJfYXV0aF91c2VyX2JhY2tlbmRxAlUOZmIuYXV0aC5GYkF1dGhxA1UNX2F1dGhfdXNl\ncl9pZHEEigEEdS5hOGZlODU0MmRjYmUwNmEzODIwNjhiYzYyODc2MWQxZA==\n"
        }
    }, 
    {
        "pk": 1, 
        "model": "sites.site", 
        "fields": {
            "domain": …
Run Code Online (Sandbox Code Playgroud)

python mysql django fixtures mysql-error-1062

7
推荐指数
1
解决办法
6233
查看次数

Django压缩了迁移:NodeNotFoundError

运行Django 1.8.9.

我只是压缩了3个应用程序的迁移并进行了部署.什么./manage.py migrate时候运行,我得到了这个:

django.db.migrations.graph.NodeNotFoundError: Migration app2.0001_squashed_0019 dependencies reference nonexistent parent node (u'app1', u'0001_squashed_0028')
Run Code Online (Sandbox Code Playgroud)

app1.0001_squashed_0028但是,磁盘存在于磁盘上,正在替换迁移.Django迁移系统应该具有向前迁移所需的所有信息.

我能够通过以下方式解决这个问题:

  1. 重命名这依赖于两个新的挤压迁移app1.0001_squashed_0028*.bak,
  2. 运行迁移,
  3. 然后重命名*.bak文件,
  4. 并重新运行迁移.

迁移在本地和分阶段工作得很好,所以我猜测发生这种情况的原因是因为生产还没有及时更新预先迁移的迁移.

我找到了几张与挤压相关的问题门票,但我不确定是否有任何相同的问题.有谁知道这是一个已知的问题?我做错了什么/是否有更好的方法可以防止这种情况发生?

python django database-migration

7
推荐指数
1
解决办法
365
查看次数

VariableDoesNotExist:在u'None中查找键[val2]失败

我得到一个VariableDoesNotExist错误与下面的代码片段时obj1.pageNone.

{{ obj1.val1|default:obj1.page.val2 }}
Run Code Online (Sandbox Code Playgroud)

通常,Django模板不关心None值的属性访问.

python django django-templates

7
推荐指数
1
解决办法
7314
查看次数

监控芹菜,我该怎么用?

我正在使用Django,Celery和Django-Celery.我想监控我的任务的状态/结果,但我对如何做到这一点感到有些困惑.

我用./manage.py celeryev,./manage.py celerymon,./manage.py celerycam?我跑sudo /etc/init.d/celeryevcam start吗?

python django monitoring celery

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

如何在节点中创建请求和响应对象

我正在尝试为我的节点/快速处理程序模拟请求和响应对象。我尝试了一些模拟库,但遇到了 API 兼容性问题,这使得它们对于测试目的来说太不可靠。

我想做的是自己创建原始请求和响应对象,并将输出定向到实时连接之外的其他地方。

这是我到目前为止所拥有的:

env.mockReq = function(o){
    o = o || {};
    o.hostname = 'www.tenor.co';
    o.protocol = 'https';
    o.path = o.url;
    o.createConnection = function(){
        console.log('mockReq createConnection');
    };
    var req = new http.ClientRequest(o);
    req.url = o.url;
    req.method = o.method;
    req.headers = o.headers || {};
    return req;
};
env.mockRes = function(o){
    var res = new http.ServerResponse({
        createConnection: function(){
            console.log('mockRes createConnection');
        }
    });
    return res;
};
Run Code Online (Sandbox Code Playgroud)

这是一些测试代码:

var req = env.mockReq({method: 'GET', url: '/'});
var res = env.mockRes();


res.on('end', function(arguments){
    expect(this.statusCode).toBe(200);
    expect(this._getData().substr(-7)).toEqual('</html>'); …
Run Code Online (Sandbox Code Playgroud)

unit-testing node.js express nock

6
推荐指数
0
解决办法
2199
查看次数

为什么Ghost.py没有加载/运行我的Javascript?

Ghost.py应该运行JS:http://jeanphix.me/Ghost.py/

require.js通过http获取,但据我所知它不会运行,因为"js/main.built"永远不会被获取,并且它的指定JS文件都没有被加载.这一切都在真正的浏览器中完美运行.

In [51]: ghost = Ghost(wait_timeout=60)

In [52]: page, resources = ghost.open(url)

In [53]: [r.url for r in resources]
Out[53]: 
[PyQt4.QtCore.QUrl(u'https://example.com/#consume/283e6571bcecf34143cbd60f35e0464b'),
 PyQt4.QtCore.QUrl(u'https://example.com/css/ui.css'),
 PyQt4.QtCore.QUrl(u'https://example.com/css/colorpicker.css'),
 PyQt4.QtCore.QUrl(u'https://example.com/css/selectize.default.css'),
 PyQt4.QtCore.QUrl(u'https://example.com/css/datepicker.css'),
 PyQt4.QtCore.QUrl(u'https://example.com/css/site.css'),
 PyQt4.QtCore.QUrl(u'https://example.com/css/jquery.fileupload-ui.css'),
 PyQt4.QtCore.QUrl(u'https://example.com/css/style.css'),
 PyQt4.QtCore.QUrl(u'https://example.com/css/bootstrap.css'),
 PyQt4.QtCore.QUrl(u'https://example.com/css/redactor.css'),
 PyQt4.QtCore.QUrl(u'https://example.com/js/lib/require.js')]

In [54]: ghost.con
ghost.confirm  ghost.content  

In [54]: ghost.content
Out[54]: u'<!DOCTYPE html><html lang="en"><head>\n    <meta charset="utf-8">\n    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">\n    <title>Ving</title>\n\n    <link rel="stylesheet" href="css/ui.css">\n    <link rel="stylesheet" href="css/bootstrap.css">\n    <link rel="stylesheet" href="css/colorpicker.css">\n    <link rel="stylesheet" href="css/redactor.css">\n    <link rel="stylesheet" href="css/site.css">\n    <link rel="stylesheet" href="css/selectize.default.css">\n    <!-- My Bug fixes / overrides to work with real …
Run Code Online (Sandbox Code Playgroud)

javascript python requirejs ghost.py

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

Python 3.4中的UnboundLocalError

以下代码适用于Python 2.7,但在Python 3.4中引发了此异常:

  File "/home/sean/dev/ving/meridian/venv/src/django-testmigrate/django_testmigrate/base.py", line 70, in __getattr__
    if e:
UnboundLocalError: local variable 'e' referenced before assignment
Run Code Online (Sandbox Code Playgroud)

e虽然在同一个函数的顶部分配.我假设Python 3中有一些新的作用域规则,但我找不到任何对它们的引用.

这是代码:

def __getattr__(self, name):
    e = None

    if not self._store:
        raise AttributeError("'%s' object has no attribute '%s'" % (self.__class__.__name__, name))

    for state, scope in reversed(list(self._store.items())):
        try:
            val = getattr(scope, name)
        except AttributeError as e:
            continue
        else:
            e = None

            # get fresh instance
            if state != self._current_state and isinstance(val, models.Model):
                model_meta = val.__class__._meta
                model_class = self._current_state.get_model(model_meta.app_label, model_meta.model_name)
                val = …
Run Code Online (Sandbox Code Playgroud)

python

3
推荐指数
1
解决办法
388
查看次数