2024年9月5日木曜日
WordPress: プラグインBogoを利用して翻訳した投稿を他のユーザーが編集できない場合
2024年7月31日水曜日
Django: プロジェクト全体でテンプレートタグを使いたい
以下のような構成とします。
/myproject
/myproject/myproject
/myproject/myapp
/myproject/templates
/myproject/templatetags/__init__.py
/myproject/templatetags/custom_tags.py
settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')], #/myproject/templates
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
'libraries': {
'custom_tags': 'templatetags.custom_tags', #myproject/myproject以下にしたい場合はmyproject.templatetags.custom_tags とすればよい
}
},
},
]
テンプレートの方で {% load custom_tags %} とすれば、自作テンプレートタグがプロジェクトで共用できるようになる。
2024年7月4日木曜日
MySQL プライマリキーのインクリメントをリセットする
MySQLでは、以下の手順でincrementを1に初期化できます。
MySQLのDBに接続して以下のalterコマンドを実行します。
$ use <データベース名>;
$ alter table <テーブル名> auto_increment = 1;
auto_increment = 10にすると、10から始まります。
参考 Djangoノウハウ集【データベース操作編】
https://sinyblog.com/django/knowledge/
Django: 今日の日付 datetime.date.today() が1日前になってしまう
settings.py で、タイムゾーン設定 USE_TZ が True になっている場合、DjangoはデフォルトでUTCを使用します。このため、表示時に適切なタイムゾーンに変換する必要があります。
Djangoのタイムゾーン設定を確認する:
# settings.py
USE_TZ = True # タイムゾーン対応を有効にする
TIME_ZONE = 'Asia/Tokyo' # サーバーのデフォルトタイムゾーンを設定する
2024年7月1日月曜日
Django: フォームのウィジエットテンプレート上書き
INSTALLED_APPS = [
'app.apps.AppConfig', # マイアプリケーション
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.forms', # 追加1
]
FORM_RENDERER = 'django.forms.renderers.TemplatesSetting' # 追加2
/myproject/app_name/templates/django/forms/widgets/select_option.html
2024年6月25日火曜日
Django: django-debug-toolbarのパネルが表示されない
django-debug-toolbarを設定したところ右サイドバーは表示されたが、右サイドバー項目をクリックしたときメインの画面が表示されず困りました。
Django管理画面では表示されるのですが、公開ページでは表示されないという現象。
調べたところ、公開ページでページ内スクロールするJavaScriptコードがdjango-debug-toolbarとかぶっていたのが原因でした。
$('a[href^="#"]').click(function () { //・・・ }
一例ですが、以下のように公開ページのイベントの動作範囲を制限して表示できるようになりました。
$('.anker a[href^="#"]').click(function () { //・・・ }
参考:django-debug-toolbarが動かない - エンジニアもどきの技術メモ
https://chantsune.github.io/articles/122/
2024年5月11日土曜日
Linux findとgrepでファイル内文字列検索
# 現在のディレクトリ以下にある「xyz*」ファイル内に「aiueo」が含まれているものを検索する
$ find . -name 'xyz*' -type f -print0 | xargs -0 grep aiueo
# 現在のディレクトリ以下にあるphpファイル内に「aiueo」が含まれているものを検索する
$ find . -name '*.php' -print0 | xargs -0 grep aiueo
# /var/www/htmlディレクトリ以下にあるファイル内に「aiueo」が含まれているものを検索する
$ find /var/www/html -type f -print0 | xargs -0 grep aiueo