Как решить проблему django.utils.datastructures.MultiValueDictKeyError: 'image'

details.html

<html>
    <head>
        <title>
        Python button script
        </title>
    </head>
    <body>
        <h1>Hello</h1>
        {% if Button_number != 0  %}
        <form method="POST">
            {% csrf_token %}
            <select name="buttonColor" id="buttonColor">
              <option selected disabled="true">Select color</option>
              <option value="Red">Red</option>
              <option value="Blue">Blue</option>
              <option value="Green">Green</option>
            </select>
            <input type="submit" value="Insert" />
        </form>
        {% endif %}
    </body>
</html>



home.html

<!DOCTYPE html>
<html>
    <head>
        <title>
        Python button script
        </title>
    </head>
    <body>

        <form action="/external/" method="post" enctype="multipart/form-data">
        {% csrf_token %}
        Input Image:
        <input type="file" name="image" required>
        <br><br>
        <input type="submit" value="Enter Image">
        </form>
    </body>
</html>

views.py

from django.shortcuts import render
from django.core.files.storage import FileSystemStorage
import pytesseract
from PIL import Image
from .models import insertbutton
from django.contrib import messages


def button(request):
    return render(request,'home.html')


def external(request):
    imagetry=request.FILES['image']
    print("image is ",imagetry)
    fs=FileSystemStorage()
    filename=fs.save(imagetry.name,imagetry)
    print(filename)
    im = Image.open(imagetry)
    text = pytesseract.image_to_string(im)
    #-------------image processing---------------
    return render(request, 'details.html', {'Button_number': Button_number, 'button_heights': button_heights, 'button_widths': button_widths })


def allData(request):
    if request.method == "POST":
        if request.POST.get('buttonColor'):
            saveValue = insertbutton()
            saveValue.buttonColor = request.POST.get('buttonColor')
            saveValue.save()
            return render(request, 'another.html')
    else:
            return render(request, 'another.html')

urls.py

from . import views
urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.button, name='home'),
    path('external/', views.external, name='script'),
    path('external/details', views.external, name='script'),
    path('another/', views.allData),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Ошибка После вставки цвета.

[04/Sep/2021 02:17:07] "POST /external/ HTTP/1.1" 200 704
Internal Server Error: /external/
Traceback (most recent call last):
  File "/home/sayma/PycharmProjects/MainProject/venv/lib/python3.8/site-packages/django/utils/datastructures.py", line 76, in __getitem__
    list_ = super().__getitem__(key)
KeyError: 'image'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/sayma/PycharmProjects/MainProject/venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/home/sayma/PycharmProjects/MainProject/venv/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/sayma/PycharmProjects/MainProject (copy)/webtoapp/webtoapp/views.py", line 14, in external
    imagetry=request.FILES['image']
  File "/home/sayma/PycharmProjects/MainProject/venv/lib/python3.8/site-packages/django/utils/datastructures.py", line 78, in __getitem__
    raise MultiValueDictKeyError(key)
django.utils.datastructures.MultiValueDictKeyError: 'image'
[04/Sep/2021 02:17:10] "POST /external/ HTTP/1.1" 500 76574

После обработки изображения и выбора соответствующего варианта я хочу сохранить эти данные в sqlite. После вставки изображения url является /external, а после вставки цвета url по-прежнему является /external. Как я могу перенаправить его на allData? Я просто хочу сохранить цвет.

Вернуться на верх