小编Iwa*_*993的帖子

Django模板过滤器语法错误

我正在尝试使用此代码使用django内置的"默认"过滤器

{% load sekizai_tags static compress i18n %}
[...]
<title>{{ title|default:"nothing" }}</title>
Run Code Online (Sandbox Code Playgroud)

但它给了我以下例外

django.template.base.TemplateSyntaxError: default requires 2 arguments, 1 provided
Run Code Online (Sandbox Code Playgroud)

我在模板后端使用以下设置

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            str(APPS_DIR.path('templates')),
        ],
        'OPTIONS': {
            'debug': DEBUG,
            'loaders': [
                'django.template.loaders.filesystem.Loader',
                'django.template.loaders.app_directories.Loader',
            ],
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'allauth.account.context_processors.account',
                'allauth.socialaccount.context_processors.socialaccount',
                'django.template.context_processors.i18n',
                'django.template.context_processors.media',
                'django.template.context_processors.static',
                'django.template.context_processors.tz',
                'django.contrib.messages.context_processors.messages',
                'sekizai.context_processors.sekizai',
            ],
        },
    },
]
Run Code Online (Sandbox Code Playgroud)

我的编辑器将代码标记为无效,但我检查了几千次 https://docs.djangoproject.com/en/1.8/ref/templates/builtins/

这是作为例子给出的:

{{ value|default:"nothing" }}
Run Code Online (Sandbox Code Playgroud)

我还尝试更改title var的名称,以确保它不是保留关键字.

django templates filter built-in

28
推荐指数
1
解决办法
7650
查看次数

Django测试客户端方法覆盖标头

我试图在我的视图集上测试我的更新方法.视图集是取自drf的模型视图集.要更新我需要发送一个put请求.由于并不总是支持这两种方法告诉服务器我正在发出一个put请求,第一种不符合我需要的方法是使用一个额外的字段来调用_method并设置它put.由于我需要发布json数据,我需要使用第二种方式,它使用X-HTTP-Method-Override标头.

要在测试用例中发布我的数据,请使用以下代码:

header = {'X_HTTP_METHOD_OVERRIDE': 'PUT'}
response = client.post('/model/1/', content_type='application/json', data=post_data_clean, **header)
Run Code Online (Sandbox Code Playgroud)

但不幸的是,我得到的结果是{'detail':'Method POST not allowed.'}.我使用插件(Postman)测试了服务器的行为,我也指定了X-HTTP-Method-Override标头.没有例外.我现在需要知道如何正确地将标头传递给django测试客户端,否则测试会在这里变得非常烦人.

python django put django-rest-framework x-http-method-override

12
推荐指数
2
解决办法
7089
查看次数

由于性能,避免深度复制

我有一个列表,其中包含另外3个列表.我需要在列表上做一些操作,因为我有180.000的那些深度复制步骤已经需要2,5s来复制列表一次.包括操作在内的总时间在220s计算时间内需要80s.

s = [[1,2,3],
     [4,5,6],
     [7,8,9]]
s1 = copy.deepcopy(s)
s1[0][0] = s[1][1]
s1[1][1] = s[0][0]
Run Code Online (Sandbox Code Playgroud)

所示的操作需要重复一百万次.因此深度复制使我面临性能瓶颈.

是否有一种更高效的方式或不同的方法来"反对"清单s

python performance deep-copy

3
推荐指数
2
解决办法
2129
查看次数

如何对不同类型的列表进行排序?

我需要使用 python 3 对列表进行排序。可能有stringsintegersfloats、 或tuples等。

我目前正在尝试sort使用如下key参数正确使用该函数:

data.sort(key=gen_key)

...

def gen_key(self, value):
        if is_number(value):
            return str(value)

        if isinstance(value, str):
            return value
    return '___' + type(value).__name__
Run Code Online (Sandbox Code Playgroud)

但问题是数字现在将自然排序。虽然我想对数字和浮点数进行排序,但仍然像数字和浮点数一样,而不是将它们视为字符串。

该行为是由return str(value)零件引起的。但是我不能返回与字符串不同的类型,因为这会引发异常,因为从 python 3 开始,字符串不会像在 python 2 中那样用数字排序。异常如下

unordarable types: int() < str()
Run Code Online (Sandbox Code Playgroud)

有什么建议?

python sorting python-3.x

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

http服务器响应(套接字)的标头和内容之间不同

我想知道,有没有可能找出响应流中标题结束的位置?

问题的背景如下,我在c中使用套接字从网站获取内容,内容以gzip编码.我想直接从流中读取内容并使用zlib对gzip内容进行编码.但我如何知道gzip内容已启动且http标头已完成.

在我看来,我大致尝试了两种给我一些奇怪结果的方法.首先,我在整个流中读取,并在终端中打印出来,我的http标题以"\ r \n \n \n \n"结束,就像我预期的那样,但是时间紧迫,我只需要检索一次响应以获取标题然后使用while循环读取内容,此处标题结束时不带"\ r \n \n \n \n".

为什么?哪种方式是阅读内容的正确方法?

我只是给你代码,这样你就可以看到我是如何从服务器获得响应的.

//first way (gives rnrn)
char *output, *output_header, *output_content, **output_result;
size_t size;
FILE *stream;
stream = open_memstream (&output, &size);
char BUF[BUFSIZ];
while(recv(socket_desc, BUF, (BUFSIZ - 1), 0) > 0)
{
    fprintf (stream, "%s", BUF);
}
fflush(stream);
fclose(stream);

output_result = str_split(output, "\r\n\r\n");
output_header = output_result[0];
output_content = output_result[1];

printf("Header:\n%s\n", output_header);
printf("Content:\n%s\n", output_content);
Run Code Online (Sandbox Code Playgroud)

.

//second way (doesnt give rnrn)
char *content, *output_header;
size_t size;
FILE *stream;
stream = …
Run Code Online (Sandbox Code Playgroud)

c sockets gzip http

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