Я очень стараюсь, но я не нашел то, что мне не хватает. Я сделал форму, которая содержит поля, такие как имя, фамилия, адрес, город, штат, почтовый индекс и один файл изображения от пользователя.
Но я получаю эту ошибку, когда я пытался опубликовать данные по просмотрам.
MultiValueDictKeyError at /submit_your_task
'image'
Request Method: POST
Request URL: http://127.0.0.1:8000/submit_your_task
Django Version: 2.2.4
Exception Type: MultiValueDictKeyError
Exception Value:
'image'
и это мои views.py для этой формы
def submit_your_task(request):
if request.method == 'POST':
first_name = request.POST['first_name']
last_name = request.POST['last_name']
address = request.POST['address']
phone = request.POST['phone']
city = request.POST['city']
state = request.POST['state']
zip = request.POST['zip']
image = request.FILES['image']
fs = FileSystemStorage()
file = fs.save(image.name, image)
url = fs.url(file)
submit_task = SubmitYourTask(first_name=first_name, last_name=last_name,
address=address,
phone=phone, city=city,
state=state, zip=zip, image=url)
submit_task.save()
return render(request, 'submit_task.html')
и это urls.py
from django.urls import path
from . import views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('', views.home, name='home-page'),
path('about/', views.about_us, name='about'),
path('signup/', views.signup, name='signup'),
path('submit_your_task/', views.submit_your_task, name='submit_your_task'),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
И когда я ставлю sla sh после 'submit_your_task' выдает мне RuntimeError
RuntimeError at /submit_your_task
You called this URL via POST, but the URL doesn't end in a slash and you have APPEND_SLASH set. Django can't redirect to the slash URL while maintaining POST data. Change your form to point to 127.0.0.1:8000/submit_your_task/ (note the trailing slash), or set APPEND_SLASH=False in your Django settings.
Request Method: POST
Request URL: http://127.0.0.1:8000/submit_your_task
Django Version: 2.2.4
Exception Type: RuntimeError
Exception Value:
You called this URL via POST, but the URL doesn't end in a slash and you have APPEND_SLASH set. Django can't redirect to the slash URL while maintaining POST data. Change your form to point to 127.0.0.1:8000/submit_your_task/ (note the trailing slash), or set APPEND_SLASH=False in your Django settings.