### Returning HttpResponseBadRequest with ajax_request Source: https://github.com/beproud/bpcommons/blob/master/docs/ja/source/topics/ajax.rst This example demonstrates how to return an `HttpResponse` object directly from a view decorated with `ajax_request`. In such cases, the decorator passes the `HttpResponse` through without attempting JSON conversion, allowing for custom responses like error messages. ```python from django.contrib.auth.models import User from commons.views.decorators import ajax_request from django.http import HttpResponseBadRequest @ajax_request def myajax_view(request): user = User.object.get(pk=request.GET["id"]) if user != request.user: return HttpResponseBadRequest(u"不正なリクエスト") return { "username": user.username, "fullname": user.get_full_name(), } ``` -------------------------------- ### ChangeLog.rst Update Source: https://github.com/beproud/bpcommons/blob/master/checklist.rst Instructions to update the next release version in the ChangeLog.rst file. ```rst .. ChangeLog.rst next version ``` -------------------------------- ### bpcommons Release History and Version Support Source: https://github.com/beproud/bpcommons/blob/master/ChangeLog.rst This section details the version compatibility of the bpcommons project with Python and Django across its releases. It highlights supported versions, dropped versions, and dependency changes. ```python Release 0.42 (2023-01-24) - Support Django-4.1 - Support Python-3.8, 3.9, 3.10, 3.11 - Drop Django-2.2 - Drop Python-3.6, 3.7 ``` ```python Release 0.41 (2022-01-25) - Support Django-3.2 - Support Python-3.7, 3.8, 3.9, 3.10 - Drop Django-1.11 ``` ```python Release 0.40 (2020-04-28) - Fix: Field.rel and Field.remote_field.to are removed in Django 2.0 ``` ```python Release 0.39 (2019-09-17) Supports: - Python-3.6, 3.7 (+ 3.7 / - 2.7) - Django-1.11, 2.2 (+ 2.2 / - 1.8, 1.10 that doesn't support py36) - Drop dependency: zenhan ``` ```python Release 0.38 (2017-07-11) - Support Python-3.6 - Support Django-1.11 - Drop Django-1.9 - Remove dependency to bputils ``` ```python Release 0.37 (2016-11-25) - Support Django-1.10 - Drop Python-2.6 - Drop Django-1.7 or earlier. - Deprecate some classes: - beproud.django.commons.http.JsonResponse - beproud.django.commons.utils.javascript.DjangoJSONEncoder - Drop some classes: - beproud.django.commons.models.fields.JSONField - beproud.django.commons.models.fields.PickledObjectField - beproud.django.commons.forms.JSONField - beproud.django.commons.forms.widgets.JSONWidget - beproud.django.commons.forms.widgets.AdminJSONWidget - Drop ``switch`` templatetag. - Remove 'BigIntegerField' from __all__ of beproud.django.commons.models.fields. Please use django.db.models.BigIntegerField instead. Please use https://pypi.python.org/pypi/jsonfield instead of our JSONField. ``` ```python Release 0.35 (2016-01-15) - Support Django-1.9 ``` -------------------------------- ### Twine Upload Command Source: https://github.com/beproud/bpcommons/blob/master/checklist.rst Command to upload the distribution files to the Python Package Index (PyPI). ```python twine upload dist/* ``` -------------------------------- ### Python Release Command Source: https://github.com/beproud/bpcommons/blob/master/checklist.rst Command to release the bpcommons project, creating source distribution and wheel files. ```python python setup.py release sdist bdist_wheel ``` -------------------------------- ### キャッシュ削除コマンド (rmcache) Source: https://github.com/beproud/bpcommons/blob/master/docs/ja/source/management/commands.rst 指定されたキャッシュキーを削除するコマンドです。主に、デプロイ時に古いキャッシュデータを削除する際に使用されます。 ```shell python manage.py rmcache mycachekey ``` -------------------------------- ### Git Tagging Command Source: https://github.com/beproud/bpcommons/blob/master/checklist.rst Command to create a Git tag for the new release version. ```git git tag v0.40 ``` -------------------------------- ### Python Package Version Update Source: https://github.com/beproud/bpcommons/blob/master/checklist.rst Instructions to update the next release version within the bpcommons Python package initialization file. ```python bpcommons/beproud/django/commons/__init__.py next version ``` -------------------------------- ### Twine Check Command Source: https://github.com/beproud/bpcommons/blob/master/checklist.rst Command to check the integrity of the distribution files before uploading to PyPI. ```python twine check dist/* ``` -------------------------------- ### FullWidthCharField - 全角文字フィールド Source: https://github.com/beproud/bpcommons/blob/master/docs/ja/source/forms/fields.rst FullWidthCharFieldは、フィールドの値が全角文字のみで構成されていることを検証します。現在、UTF-8エンコーディングのみをサポートしています。 ```python from django import forms from commons.forms import FullWidthCharField class MyForm(forms.Form): name = FullWidthCharField(label=u'名前') ``` -------------------------------- ### Standard Django AJAX View Source: https://github.com/beproud/bpcommons/blob/master/docs/ja/source/topics/ajax.rst This snippet demonstrates the traditional way to handle AJAX requests in Django, requiring manual JSON serialization and mimetype specification. ```python from django.contrib.auth.models import User from django.utils import simplejson def myajax_view(request): user = User.object.get(pk=request.GET["id"]) return HttpResponse(simplejson.dumps({ "username": user.username, "fullname": user.get_full_name(), }), mimetype="text/javascript") ``` -------------------------------- ### JSONField - 非推奨 Source: https://github.com/beproud/bpcommons/blob/master/docs/ja/source/forms/fields.rst JSONFieldは非推奨となり、削除されました。代わりに `jsonfield` ライブラリ(https://pypi.org/project/jsonfield/)を使用してください。 -------------------------------- ### Using ajax_request Decorator for JSON Response Source: https://github.com/beproud/bpcommons/blob/master/docs/ja/source/topics/ajax.rst This snippet shows how to use the `ajax_request` decorator to automatically convert a Python dictionary returned by a view into a JSON response. This simplifies the process and handles serialization of various data types. ```python from django.contrib.auth.models import User from commons.views.decorators import ajax_request @ajax_request def myajax_view(request): user = User.object.get(pk=request.GET["id"]) return { "username": user.username, "fullname": user.get_full_name(), } ``` -------------------------------- ### StripRegexField - 正規表現検証付きフィールド Source: https://github.com/beproud/bpcommons/blob/master/docs/ja/source/forms/fields.rst StripRegexFieldは、検証前にフィールドの値から空白文字を削除し、指定された正規表現にマッチするかどうかを検証します。これは、ユーザー入力をクリーンアップし、特定のパターンに準拠させるのに役立ちます。 ```python from django import forms from commons.forms import StripRegexField class MyForm(forms.Form): name = StripRegexField('^Monty', label=u'名前', error_message=u'名前はMontyから始まらないといけません。') ``` -------------------------------- ### Django Template Tag: to_anchortrunc Source: https://github.com/beproud/bpcommons/blob/master/tests/templates/templatetags_tests/to_anchortrunc.html Filters a string to truncate it and create an anchor link. It takes a maximum length as an argument. ```django {% load html_tags %} {{ my_data|to_anchortrunc:15 }} ``` -------------------------------- ### NumCharField - 数字のみを許可するフィールド Source: https://github.com/beproud/bpcommons/blob/master/docs/ja/source/forms/fields.rst NumCharFieldは、フィールドの値が数字のみで構成されていることを検証します。伝票IDや数値コードなどの入力に便利です。 ```python from django import forms from commons.forms import NumCharField class MyForm(forms.Form): voucher_id = NumCharField(label=u'伝票ID') ``` -------------------------------- ### Django Template Tag for Anchor Links Source: https://github.com/beproud/bpcommons/blob/master/tests/templates/templatetags_tests/to_anchor.html This snippet demonstrates the usage of the 'to_anchor' template filter from the 'html_tags' library in Django. It's used to generate anchor links from data. ```django {% load html_tags %} {{ my_data|to_anchor }} ``` -------------------------------- ### HiraganaCharField - 全角ひらがなフィールド Source: https://github.com/beproud/bpcommons/blob/master/docs/ja/source/forms/fields.rst HiraganaCharFieldは、フィールドの値が全角ひらがなのみで構成されていることを検証します。現在、UTF-8エンコーディングのみをサポートしています。 ```python from django import forms from commons.forms import HiraganaCharField class MyForm(forms.Form): name = HiraganaCharField(label=u'名前') ``` -------------------------------- ### EmailField - 拡張電子メール検証 Source: https://github.com/beproud/bpcommons/blob/master/docs/ja/source/forms/fields.rst このEmailFieldは、Djangoの標準EmailFieldよりも寛容な電子メールアドレス検証を提供し、特に日本の携帯電話メールアドレスのようなフォーマットをサポートします。DjangoのEmailFieldと同様に使用できます。 ```python from django import forms from commons.forms import EmailField class MyForm(forms.Form): email = EmailField(label=u"メールアドレス") ``` -------------------------------- ### AlphaNumField - 英数字とアンダースコア/ハイフンフィールド Source: https://github.com/beproud/bpcommons/blob/master/docs/ja/source/forms/fields.rst AlphaNumFieldは、半角英数字、アンダースコア(_)、およびハイフン(-)のみを許可するフィールドです。ユーザー名やIDなどの入力検証に適しています。 ```python from django import forms from commons.forms import AlphaNumField class MyForm(forms.Form): username = AlphaNumField(label=u'ユーザ名') ``` -------------------------------- ### Abbreviate String in Django Templates Source: https://github.com/beproud/bpcommons/blob/master/tests/templates/templatetags_tests/string_tags.html This snippet demonstrates how to use the 'abbrev' template filter from the 'string_tags' library to shorten strings within a Django template. It takes a string variable and an integer specifying the maximum length. ```django {% load string_tags %} {{ my_data|abbrev:10 }} ``` -------------------------------- ### Abbreviate String Filter Source: https://github.com/beproud/bpcommons/blob/master/tests/templates/templatetags_tests/string_tags2.html The `abbrev` filter from `string_tags` is used to truncate a string to a specified length, appending an ellipsis if truncation occurs. It's useful for displaying long strings concisely in templates. ```django {% load string_tags %} {% filter abbrev:10 %}{{ my_data }}{% endfilter %} ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.