Environment:
Request Method: POST
Request URL:
127.0.0.1:8000/add_article
Django Version: 3.0.3
Python Version: 3.6.9
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'office']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback (most recent call last):
File "/home/mrrobot/PycharmProjects/test_art_db/venv/lib/python3.6/site-packages/django/utils/datastructures.py", line 76, in __getitem__
list_ = super().__getitem__(key)
During handling of the above exception ('photo'), another exception occurred:
File "/home/mrrobot/PycharmProjects/test_art_db/venv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/mrrobot/PycharmProjects/test_art_db/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/mrrobot/PycharmProjects/test_art_db/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/mrrobot/PycharmProjects/test_art_db/venv/lib/python3.6/site-packages/django/contrib/auth/decorators.py", line 21, in _wrapped_view
return view_func(request, *args, **kwargs)
File "/home/mrrobot/PycharmProjects/art_backup/test_articles/office/views.py", line 26, in write_article
article.photo = request.FILES['photo']
File "/home/mrrobot/PycharmProjects/test_art_db/venv/lib/python3.6/site-packages/django/utils/datastructures.py", line 78, in __getitem__
raise MultiValueDictKeyError(key)
Exception Type: MultiValueDictKeyError at /add_article
Exception Value: 'photo'
views
def write_article(request):
form = ArticleForm
if request.method == 'POST':
article = Article()
article.title = request.POST.get('title')
article.prev = request.POST.get('prev')
article.text = request.POST.get('text')
article.photo = request.FILES['photo']
article.create_preview()
article.save()
return HttpResponseRedirect('/')
return render(request, 'add_article.html', context={'form': form})
models
class Article(models.Model):
title = models.CharField(max_length=200, default='')
prev = models.TextField(blank=True, max_length=200, default='')
text = models.TextField()
created_date = models.DateTimeField(default=timezone.now)
photo = models.ImageField(blank=True, upload_to='articles_images', default=None)
Подскажите, пожалуйста, в чём проблема?