小编Dog*_*kci的帖子

如何获取Flask请求的URL的不同部分?

我想检测,如果该请求来自来到localhost:5000或foo.herokuapp.com主机,并请走什么路.如何获取有关Flask请求的此信息?

python url flask

120
推荐指数
3
解决办法
10万
查看次数

SQLAlchemy Flask过滤器查询结合两个模型的结果

我试图通过date_registered订购用户的感谢(帖子).

目前,有两个查询可以获得两个列表,然后手动组合并重新排序.用户可以是感谢者或接收者.感谢总是一个单独的给予者,但可能有多个接收者.因此我有一个Thank和一个ThankReceivedByUser模型来获得结果.

class Thank(db.Model):

    id = db.Column(db.Integer, primary_key = True)
    giver_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable = False)
    status = db.Column(db.SmallInteger, nullable = False) 
    date_registered = db.Column(db.DateTime, nullable = False)

class ThankReceivedByUser(db.Model):

    id = db.Column(db.Integer, primary_key = True)
    thank_id = db.Column(db.Integer, db.ForeignKey("thank.id"), nullable = False)
    receiver_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable = False)
    status = db.Column(db.SmallInteger, nullable = False) 
    date_registered = db.Column(db.DateTime, nullable = False)
Run Code Online (Sandbox Code Playgroud)

如果可能的话,在构建单个查询时获得一些提示会很棒.

python orm sqlalchemy flask flask-sqlalchemy

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

Django Translations compilemessages错误:'msgstr'不是有效的Python格式字符串

由于%符号,我有以下错误.知道如何克服这个错误吗?

django-admin.py compilemessages -l tr

在/.../locale/tr/LC_MESSAGES处理文件django.po

CommandError:执行msgfmt失败:/.../locale/tr/LC_MESSAGES/django.po:139:'msgstr'不是有效的Python格式字符串,与'msgid'不同.原因:在指令编号1中,字符"{"不是有效的转换说明符.

/.../locale/tr/LC_MESSAGES/django.po:146:'msgstr'不是有效的Python格式字符串,与'msgid'不同.原因:在指令编号1中,字符"{"不是有效的转换说明符.msgfmt:发现2个致命错误

models.py

def get_full_title(self):
    return _(u"{discounted_price} TL for {original_price} TL worth of {product_name} at {place_name} {locality} {city} ({discount_percentage}% off)").format(
        discounted_price=int(self.discounted_price),
        original_price=int(self.original_price),
        product_name=self.product.name,
        place_name=self.product.place.name,
        locality=self.product.place.locality,
        city=self.product.place.get_city_display(),
        discount_percentage=self.get_rounded_discount_percentage(),
    )


def get_short_title(self):
    return _(u"{product_name} at {place_name} ({discount_percentage}% off)").format(
        product_name=self.product.name,
        place_name=self.product.place.name,
        discount_percentage=self.get_rounded_discount_percentage(),
    )
Run Code Online (Sandbox Code Playgroud)

django.po

#: deals/models.py:169
#, python-format, python-brace-format
msgid ""
"{discounted_price} TL for {original_price} TL worth of {product_name} at "
"{place_name} {locality} {city} ({discount_percentage}% off)"
msgstr ""
"{discounted_price} TL for {original_price} TL worth of {product_name} at …
Run Code Online (Sandbox Code Playgroud)

python django translation

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

Django Heroku Postgresql错误:编码"UTF8"的无效字节序列

我正在使用Django和Heroku Postgreql.我在默认数据库设置上使用英语和土耳其语.我的数据库在本地和Heroku上都没有问题.但是,当我尝试将db从本地转储并恢复到生产时,我收到错误.

psql -U user -d db_name -f "b003.dump"

psql:b003.dump:270: ERROR:  invalid byte sequence for encoding "UTF8": 0xa3
Run Code Online (Sandbox Code Playgroud)

django postgresql database-restore heroku-postgres

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

这条长Python系列的正确PEP8练习是什么?

我应该如何分解这条线,以便它遵守PEP8?

    assert (sum(map(lambda x: len(x), 
                    (activities,apps,classes,users,verbs))) ==
            Object.query
                  .filter(Object.status != ObjectStatusChoices.DELETED)
                  .count())
Run Code Online (Sandbox Code Playgroud)

python pep8 python-2.7

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

Django 模型表单图像字段未验证

我正在使用 Django 模型表单编写一个简单的视图,但是图像字段无法验证,并显示“此字段是必需的”错误消息。

我想知道问题出在哪里...

模型:

class Deal(AbstractModel):
    IMAGE_MAX_LENGTH = 200

    place = models.ForeignKey(Place, related_name='deals', related_query_name='deal', verbose_name=_("Place"))
    image = models.ImageField(default='deals/default.png', max_length=IMAGE_MAX_LENGTH, upload_to='deals', verbose_name=_("Image"))
    ...
Run Code Online (Sandbox Code Playgroud)

看法:

@login_required
def deals_create(request):
    # Get place id
    place_id = request.GET.get('place')
    # Get place
    place = Place.objects.get(id=place_id)
    # Process form data
    if request.method == 'POST':
        form = DealsCreateForm(request.POST, request.FILES)
        # Validate post data
        if form.is_valid():
            # Save deal data
            deal = form.save(commit=False)
            deal.place = place
            deal.save()
            # Redirect to reload page and clear post data
            return HttpResponseRedirect(reverse('deal_manager:deals_pending')) …
Run Code Online (Sandbox Code Playgroud)

django django-forms django-file-upload

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