前回の質問と内容は同じですが、ホームページ内の登録画面で画像を登録し、一覧画面で出力が行えるプログラムを作成しています。作成段階でAssertionError:に遭遇しました。
サイトを参考にしたところ、ImageFieldに該当するモデルをforms.pyのfieldに入れない事とhtmlソースにenctype="multipart/form-data"を入れる必要があることの2つが分かったので、修正したのですがエラーは解決しませんでした。ご教授の方お願いします。
models.py
python
1class Item(models.Model): 2 """ 3 データ定義クラス 4 各フィールドを定義する 5 参考: 6 ・公式 モデルフィールドリファレンス 7 https://docs.djangoproject.com/ja/2.1/ref/models/fields/ 8 """ 9 10 11 name = models.CharField( 12 verbose_name='ラーメン店', 13 max_length=20, 14 blank=True, 15 null=True, 16 ) 17 18 name1 = models.CharField( 19 verbose_name='メニュー名称', 20 max_length=20, 21 blank=True, 22 null=True, 23 ) 24 25 image = models.ImageField(upload_to='img/upload') 26 27 28 description = models.CharField( 29 null=True, 30 blank=True, 31 max_length=256, 32 ) 33 34
item_filter.html
python
1 <form method="post" enctype="multipart/form-data"> 2{% extends "./_base.html" %} 3{% block content %} 4 {% load crispy_forms_tags %} 5 <div class="container"> 6 <div id="myModal" class="modal fade" tabindex="-1" role="dialog"> 7 <div class="modal-dialog" role="document"> 8 <div class="modal-content"> 9 <div class="modal-header"> 10 <h5 class="modal-title">検索条件</h5> 11 <button type="button" class="close" data-dismiss="modal" aria-label="閉じる"> 12 <span aria-hidden="true">×</span> 13 </button> 14 </div> 15 <form id="filter" method="get"> 16 <div class="modal-body"> 17 {{ filter.form|crispy }} 18 </div> 19 </form>
setting.py
python
1# Static files (CSS, JavaScript, Images) 2# https://docs.djangoproject.com/en/2.0/howto/static-files/ 3STATIC_URL = '/static/' 4 5#画像格納設定 6MEDIA_URL = '/media/' 7MEDIA_ROOT = os.path.join(BASE_DIR, 'media') 8 9# カスタムユーザーモデルの使用 10# https://docs.djangoproject.com/ja/2.1/topics/auth/customizing/#substituting-a-custom-user-model 11AUTH_USER_MODEL = 'users.User'
forms.py
python
1from django import forms 2 3from .models import Item 4 5 6class ItemForm(forms.ModelForm): 7 """ 8 モデルフォーム構成クラス 9 ・公式 モデルからフォームを作成する 10 https://docs.djangoproject.com/ja/2.1/topics/forms/modelforms/ 11 """ 12 13 class Meta: 14 model = Item 15 fields = ("name","name1","description")
エラー内容
python
1AssertionError: ItemFilterSet resolved field 'image' with 'exact' lookup to an unrecognized field type ImageField. Try adding an override to 'Meta.filter_overrides'. See: https://django-filter.readthedocs.io/en/master/ref/filterset.html#customise-filter-generation-with-filter-overrides
参考サイトhttps://qiita.com/greenteabiscuit/items/2510e14d7ef2630c6249
あなたの回答
tips
プレビュー