我读了几篇关于哪个bootstrap包使用的帖子(主要是Crispy-form VS django-toolkit-integration)
但我对Django很新,我仍然不明白这些软件包的真正需求是什么.我的意思是,Twitter引导程序只不过是css/js文件.所以,我想通过将我的Django表单和字段链接到HTML类(使用.py表单的小部件,以及直接在其他字段的.html模板中)来使用bootstrap
那么,这些包的好处是什么?这只是方便还是我真的错过了一些东西,如果我选择不使用它?
谢谢你的帮助!
我按照本教程使用Django设置Amazon S3.但是当我使用Python 3.3时,我安装了一个兼容Python-3的django- storages和boto3的分支.
这是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) 我在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的后端方法,我想强制或不强制信用卡字段.我怎样才能做到这一点?谢谢.
运行时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) 我不明白 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>\nRun 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 }\nRun Code Online (Sandbox Code Playgroud)\n\n在构造函数中,creditAmount 是一个数字。在里面onChangeCreditAmount方法中,它变成了一个字符串。
我发现的唯一解决方法是使用显式强制转换:Number(this.creditAmount)但这对我来说似乎很棘手。有没有更好的办法 ?谢谢。
我想使用--keepdb选项,允许在运行测试后保留数据库,因为在我的情况下,创建数据库需要一段时间(但运行测试实际上很快)
但是,我想只保留数据库结构,而不是数据。每次运行测试时,我都需要一个空数据库。
我知道我可以使用tearDown方法来删除创建的每个对象,但这是一种乏味且容易出错的方法。我只需要找到一种方法告诉 Django 在单元测试结束时刷新整个 dabatase(而不是销毁它)。
我正在考虑制作一个非常简单的脚本:
manage.py test --keepdbmanage.py flushdb --database test_fugodb然而,通过第二步,我得到了django.db.utils.ConnectionDoesNotExist: The connection test_fugodb doesn't exist. 这个测试数据库的名称是什么?我拿了运行测试时显示的一个:
怎么了?谢谢!
在聚合查询集时,我注意到如果我之前使用注释,我会得到错误的结果。我不明白为什么。
编码
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) 我在这个答案中发现,我可以使用原始SQL轻松删除表中的重复行(基于N列的重复).
使用Django ORM是否存在等价?我在Django中发现的唯一内容仅基于1列重复.
注意:我知道有一种方法可以防止Django中的未来重复(基于几个字段),使用unique_together字段(但我以前不知道).
谢谢.
我在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
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
django ×7
python ×7
angular ×2
unit-testing ×2
amazon-s3 ×1
boto ×1
boto3 ×1
django-orm ×1
ionic2 ×1
pip ×1
typescript ×1