小编Dav*_* D.的帖子

Django:我为什么要使用一个包进行Twitter引导程序集成?

我读了几篇关于哪个bootstrap包使用的帖子(主要是Crispy-form VS django-toolkit-integration)

但我对Django很新,我仍然不明白这些软件包的真正需求是什么.我的意思是,Twitter引导程序只不过是css/js文件.所以,我想通过将我的Django表单和字段链接到HTML类(使用.py表单的小部件,以及直接在其他字段的.html模板中)来使用bootstrap

那么,这些包的好处是什么?这只是方便还是我真的错过了一些东西,如果我选择不使用它?

谢谢你的帮助!

python django django-templates twitter-bootstrap

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

Django与Amazon S3通过boto3:NotperlyConfigured

我按照本教程使用Django设置Amazon S3.但是当我使用Python 3.3时,我安装了一个兼容Python-3的django- storagesboto3的分支.

这是settings.py文件:

AWS_STORAGE_BUCKET_NAME = os.environ['LIVIN_AWS_STORAGE_BUCKET_NAME']
S3_REGION_NAME = os.environ['LIVIN_S3_REGION_NAME']
AWS_ACCESS_KEY_ID = os.environ['LIVIN_AWS_ACCESS_KEY_ID']
AWS_SECRET_ACCESS_KEY = os.environ['LIVIN_AWS_SECRET_ACCESS_KEY']

AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME
STATIC_URL = "https://%s/" % AWS_S3_CUSTOM_DOMAIN

# Tell the staticfiles app to use S3Boto storage when writing the collected
# static files (when you run `collectstatic`).
STATICFILES_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
Run Code Online (Sandbox Code Playgroud)

当我尝试时,python manage.py collectstatic我收到此错误:

ImportError: No module named 'boto'
During handling of the above exception, another exception occurred:
...
django.core.exceptions.ImproperlyConfigured: Could not …
Run Code Online (Sandbox Code Playgroud)

python django amazon-s3 boto boto3

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

使用DjangoRestFramework序列化程序的条件字段要求

我在Django Rest Framework 2中有这些嵌套的序列化器:

class BookingSerializer(Serializer):
    reservation_history_id = serializers.IntegerField()
    credit_card = 'api.v1.serializer.AddCreditCardSerializer()'
    save_credit_card = serializers.BooleanField()
    promo_code = serializers.BooleanField(required=False)

    # validation stuff here ...

class AddCreditCardSerializer(ModelSerializer):
    """ Credit card model serializer """
    expiration_date = serializers.CharField()
    card_number = serializers.CharField()
    security_code = serializers.IntegerField()

    # validation stuff here ...
Run Code Online (Sandbox Code Playgroud)

在我的ApiView中BookingApiView,我正在打电话给BookingSerializer.根据返回True或False的后端方法,我想强制或不强制信用卡字段.我怎样才能做到这一点?谢谢.

python django serialization django-rest-framework

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

无法通过pip安装py3exiv2?

运行时pip install py3exiv2,出现此错误:

Collecting py3exiv2
  Downloading py3exiv2-0.2.1.tar.gz
Installing collected packages: py3exiv2
  Running setup.py install for py3exiv2 ... error
    Complete output from command /home/vagrant/venv/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-j6a3aby9/py3exiv2/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-ppz_a46j-record/install-record.txt --single-version-externally-managed --compile --install-headers /home/vagrant/venv/include/site/python3.4/py3exiv2:
    running install
    running build
    running build_py
    creating build
    creating build/lib.linux-x86_64-3.4
    creating build/lib.linux-x86_64-3.4/pyexiv2
    copying src/pyexiv2/iptc.py -> build/lib.linux-x86_64-3.4/pyexiv2
    copying src/pyexiv2/exif.py -> build/lib.linux-x86_64-3.4/pyexiv2
    copying src/pyexiv2/xmp.py -> build/lib.linux-x86_64-3.4/pyexiv2
    copying src/pyexiv2/preview.py -> build/lib.linux-x86_64-3.4/pyexiv2
    copying src/pyexiv2/__init__.py -> build/lib.linux-x86_64-3.4/pyexiv2
    copying src/pyexiv2/utils.py -> build/lib.linux-x86_64-3.4/pyexiv2
    copying src/pyexiv2/metadata.py …
Run Code Online (Sandbox Code Playgroud)

python pip

5
推荐指数
2
解决办法
3231
查看次数

使用 NgModel 绑定将数字变成字符串

我不明白 Typescript 和 Angular 的这种行为。

\n\n

我的组件.html

\n\n
  <ion-select [(ngModel)]="creditAmount"\n              (ngModelChange)="onChangeCreditAmount()">\n    <ion-option value="25" selected="true">25\xe2\x82\xac</ion-option>\n    <ion-option value="50">50\xe2\x82\xac</ion-option>\n    <ion-option value="75">75\xe2\x82\xac</ion-option>\n    <ion-option value="100">100\xe2\x82\xac</ion-option>\n  </ion-select>\n
Run Code Online (Sandbox Code Playgroud)\n\n

我的组件.ts

\n\n
  creditAmount: number;\n\n  constructor() {\n    this.creditAmount = 25;\n    console.log(this.creditAmount);\n  }\n  onChangeCreditAmount() {\n    console.log(this.creditAmount);\n  }\n
Run Code Online (Sandbox Code Playgroud)\n\n

在构造函数中,creditAmount 是一个数字。在里面onChangeCreditAmount方法中,它变成了一个字符串。

\n\n

我发现的唯一解决方法是使用显式强制转换:Number(this.creditAmount)但这对我来说似乎很棘手。有没有更好的办法 ?谢谢。

\n

typescript angular

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

Django:运行测试后如何保持没有数据的数据库结构?

我想使用--keepdb选项,允许在运行测试后保留数据库,因为在我的情况下,创建数据库需要一段时间(但运行测试实际上很快)

但是,我想只保留数据库结构,而不是数据。每次运行测试时,我都需要一个空数据库。

我知道我可以使用tearDown方法来删除创建的每个对象,但这是一种乏味且容易出错的方法。我只需要找到一种方法告诉 Django 在单元测试结束时刷新整个 dabatase(而不是销毁它)。

我正在考虑制作一个非常简单的脚本:

  1. 运行保留数据库的测试:manage.py test --keepdb
  2. 跑步manage.py flushdb --database test_fugodb

然而,通过第二步,我得到了django.db.utils.ConnectionDoesNotExist: The connection test_fugodb doesn't exist. 这个测试数据库的名称是什么?我拿了运行测试时显示的一个:

怎么了?谢谢!

python django unit-testing

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

Django:使用注释后聚合返回错误的结果

在聚合查询集时,我注意到如果我之前使用注释,我会得到错误的结果。我不明白为什么。

编码

from django.db.models import QuerySet, Max, F, ExpressionWrapper, DecimalField, Sum
from orders.models import OrderOperation

class OrderOperationQuerySet(QuerySet):
    def last_only(self) -> QuerySet:
        return self \
            .annotate(last_oo_pk=Max('order__orderoperation__pk')) \
            .filter(pk=F('last_oo_pk'))

    @staticmethod
    def _hist_price(orderable_field):
        return ExpressionWrapper(
            F(f'{orderable_field}__hist_unit_price') * F(f'{orderable_field}__quantity'),
            output_field=DecimalField())

    def ordered_articles_data(self):
        return self.aggregate(
            sum_ordered_articles_amounts=Sum(self._hist_price('orderedarticle')))
Run Code Online (Sandbox Code Playgroud)

考试

qs1 = OrderOperation.objects.filter(order__pk=31655)
qs2 = OrderOperation.objects.filter(order__pk=31655).last_only()
assert qs1.count() == qs2.count() == 1 and qs1[0] == qs2[0]  # shows that both querysets contains the same object

qs1.ordered_articles_data()
> {'sum_ordered_articles_amounts': Decimal('3.72')}  # expected result

qs2.ordered_articles_data()
> {'sum_ordered_articles_amounts': Decimal('3.01')}  # wrong …
Run Code Online (Sandbox Code Playgroud)

python django django-queryset

5
推荐指数
0
解决办法
651
查看次数

Django:如何根据2列删除重复的行?

我在这个答案中发现,我可以使用原始SQL轻松删除表中的重复行(基于N列的重复).

使用Django ORM是否存在等价?我在Django中发现的唯一内容仅基于1列重复.

注意:我知道有一种方法可以防止Django中的未来重复(基于几个字段),使用unique_together字段(但我以前不知道).

谢谢.

python django django-orm

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

使用嵌套对象与Django Rest Framework和单元测试

我在Django Rest Framework端点上编写了几个单元测试,没有任何问题,直到我尝试在POST请求中传递嵌套对象:

class BookTestCase(APIVersion, APITestCase):
    def setUp(self):
        self.url = self.reverse_with_get_params('book')
        self.user = CustomerFactory.create().user
        self.base_data = {"foo": "bar",
                          "credit_card": {"card_number": "1234567812345678",
                                          "expiration_date": "1116",
                                          "security_code": "359"},
                          "foo2": "bar2"}

    def test_book(self):
        add_token_to_user(self.user, self.client)

        response = self.client.post(self.url, self.base_data)

        self.assertEqual(response.status_code, 200)
Run Code Online (Sandbox Code Playgroud)

然后,pdb.set_trace()在最开始使用a运行相关的Web服务,这里是以下内容request.DATA:

<QueryDict: {u'foo': [u'bar'],
             u'credit_card': [u'expiration_date', u'security_code', u'card_number'],
             u'foo2': [u'bar2']}>
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,每个level1对象都已正确填充,但信用卡内容已消失.

任何的想法?谢谢!

注意:Django 1.6/Rest Framework 2

django unit-testing django-rest-framework

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

离子和条纹元素:iOS的键盘错误

Stripe介绍了一种收集名为Stripe Elements的信用卡信息的方法.粗略地说,它包括让Stripe建议用于收集信用卡的UI.

我在Ionic3/Angular4应用程序中使用它.现在,除了我发现一个非常讨厌的bug之外,它只能在iOS中运行完美:当关注信用卡号码时,会出现一个键盘(似乎是原生的键盘),然后它会被离子键盘取代0.5秒之后,正如这两部iPhone截图所示:

这通常不会成为问题.但是,如果我按"后退"按钮,我会转到上一页,但键盘会停留!即使我关闭键盘,一旦我进入新页面,或者如果我打开菜单,它将永远重新打开...我的用户界面被打破了.

我有直觉,在任何输入上触发的离子键盘与Stripe代码触发的本机键盘之间存在冲突.但是由于条纹元素的性质,我无法控制表单的内容,我在html代码中只有这个:

<form action="/charge" method="post" id="payment-form">
      <div class="form-row">
          <div id="card-element">
            <!-- a Stripe Element will be inserted here. -->
          </div>

      ....
</form>
Run Code Online (Sandbox Code Playgroud)

知道如何尝试调试这个吗?你认为我可以告诉Ionic不要触发键盘吗?

非常感谢.注意:我正在使用离子角度3.7.1

stripe-payments ionic-framework ionic2 angular

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