小编use*_*656的帖子

如何将链接放入 django 的翻译标签中

我的问题是,如何翻译标签,标签内的链接在哪里。我正在尝试,但我只得到翻译 for ,msgid而不是 for msgid

网站.html

<label for="id_data_protection" class="checkbox">
    {% blocktrans %}Lorem <a href="/en/policy" target="_blank">Lorem ipsum</a> lorem ipsum test test {% endblocktrans %}
</label>
Run Code Online (Sandbox Code Playgroud)

django.po 文件

#: templates/pages/templates/contact.html:72
msgid ""
"Text <a href=\"/de/datenschutz\" target=\"_blank\">Text</a> "
"TextTextTextTextText"
msgstr ""
"Text <a href=\"/en/policy\" target=\"_blank\">Text Text</a> Text "
"TextTextText TextText"
Run Code Online (Sandbox Code Playgroud)

python django translation href hyperlink

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

如何在Django中测试auto_now_add

我有django 1.11应用程序,我想为我的解决方案编写单元测试。

我想测试注册日期功能。

model.py:

class User(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    registration_date = models.DateTimeField(auto_now_add=True)

    def get_registration_date(self):
        return self.registration_date
Run Code Online (Sandbox Code Playgroud)

我还在模型工厂中使用django-boy:factory.py

  class UserFactory(factory.DjangoModelFactory):
        class Meta:
            model = models.User
        first_name = 'This is first name'
        last_name = 'This is last name'
        registration_date = timezone.now()
Run Code Online (Sandbox Code Playgroud)

test.py

def test_get_registration_date(self):
    user = factories.UserFactory.create()
    self.assertEqual(user.get_registration_date(), timezone.now())
Run Code Online (Sandbox Code Playgroud)

问题是我接受了AssertionError

AssertionError: datetime.datetime(2018, 4, 17, 9, 39, 36, 707927, tzinfo=<UTC>) != datetime.datetime(2018, 4, 17, 9, 39, 36, 708069, tzinfo=<UTC>)
Run Code Online (Sandbox Code Playgroud)

python django datetime unit-testing factory-boy

4
推荐指数
3
解决办法
836
查看次数

在django admin中仅按年过滤

我想在管理面板中创建一个过滤器,它将根据年份过滤事件.我创建了一个多年的列表,但我不知道如何将它与lookup和queryset结合起来.

class SearchByYear(admin.SimpleListFilter):
    title = _('title')
    parameter_name = 'year'
    year_list = models.Event.objects.values_list('date', flat=True)

    y = [i.year for i in year_list]
    print('this is list only with years: ', y)

    def lookups(self, request, model_admin):
        return (
            ('year', _('2018')),
            ('year1', _('2019')),
        )

    def queryset(self, request, queryset):
        if self.value() == 'year':
            return queryset.filter(date__year=2018)
        if self.value() == 'year1':
            return queryset.filter(date__year=2019)
Run Code Online (Sandbox Code Playgroud)

python django filter django-admin

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

使用 bash 检查包是否早于 24 小时

我想检查我的最后一个文件是否早于 24 小时。(django 中的项目)我的目录中有很多 zip 包,所以我必须用这部分代码“过滤”最后一个:ls -1 | sort -n | tail -n1

我在 .sh 文件中的代码:

#!/bin/bash

file="$HOME/path_directory ls -1 | sort -n | tail -n1"
current=`date +%s`;
last_modified=`stat -c "%Y" $file`;


if [ $(($current-$last_modified)) -gt 86400 ]; then
     echo "File is older that 24 hours" | mailx noreply@address -s "Older than 24 hours" me@mailmail.com
else
     echo "File is up to date.";
fi;
Run Code Online (Sandbox Code Playgroud)

这是我得到的一个错误:

stat: invalid option -- '1'
Try 'stat --help' for more information.
/path_directory/imported_file.sh: line 9: …
Run Code Online (Sandbox Code Playgroud)

bash shell

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