@Sanorian

Файл не доходит до сервера Django. Как исправить?

Ошибка:

Internal Server Error: /add_book
Traceback (most recent call last):
  File "/home/sanorian/Documents/Klokov/2/venv/lib/python3.11/site-packages/django/utils/datastructures.py", line 84, in __getitem__
    list_ = super().__getitem__(key)
            ^^^^^^^^^^^^^^^^^^^^^^^^
KeyError: 'book_image'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/sanorian/Documents/Klokov/2/venv/lib/python3.11/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "/home/sanorian/Documents/Klokov/2/venv/lib/python3.11/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sanorian/Documents/Klokov/2/books/views.py", line 16, in add_book
    b = Book(name=request.POST["name"], author=request.POST["author"], description=request.POST["description"], image=request.FILES["book_image"])
                                                                                                                      ~~~~~~~~~~~~~^^^^^^^^^^^^^^
  File "/home/sanorian/Documents/Klokov/2/venv/lib/python3.11/site-packages/django/utils/datastructures.py", line 86, in __getitem__
    raise MultiValueDictKeyError(key)
django.utils.datastructures.MultiValueDictKeyError: 'book_image'
[11/May/2024 16:45:22] "POST /add_book HTTP/1.1" 500 74688

views.py:

def add_book(request):
    if request.method == "POST":
        b = Book(name=request.POST["name"], author=request.POST["author"], description=request.POST["description"], image=request.FILES["book_image"])
        b.save()
        return HttpResponseRedirect(f"/{b.pk}")
    else:
        return render(request, "books/add_book.html")

models.py:
class Book(models.Model):
    name = models.TextField(default="")
    author = models.TextField(default="")
    description = models.TextField(default="")
    image = models.ImageField(upload_to="images/")
    def __str__(self):
        return self.name

Форма на странице:

<form method="post">
            {% csrf_token %}
            <h3>Нзвание:</h3>
            <input type="text" name="name" required />
            <h3>Автор:</h3>
            <input type="text" name="author" required />
            <h3>Описание:</h3>
            <input type="text" name="description" required />
            <h3>Обложкa:</h3>
            <input type="file" name="book_image" accept="image/*" required />
            <button type="submit">Отправить</button>
        </form>

Не получается обработать файл на сервере. Он до него не доходит. В форме я его "оставляю".
  • Вопрос задан
  • 48 просмотров
Решения вопроса 1
@Everything_is_bad
потому что ты не читаешь доки https://docs.djangoproject.com/en/5.0/ref/forms/ap...
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы