Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
76 views
in Technique[技术] by (71.8m points)

python - Django app crashes when I try to submit a photo to a form

So I have a django app in which I have created a form. When I get to submitting an image on the form I click the image icon and the app automatically crashes and i get this in the terminal: django.utils.datastructures.MultiValueDictKeyError: 'photo' '''

 {% extends "auctions/layout.html" %}

{% block body %}
    <h2>Create New Listing</h2>

    <form action="{% url 'newListing' %}" method="post">
        {% csrf_token %}
        <div class="form-group">
            <input autofocus class="form-control" type="text" name="title" placeholder="Title">
        </div>
        <div class="form-group">
            <input class="form-control" type="text" name="description" placeholder="Description">
        </div>
        <div class="form-group">
            <input class="form-control" type="number" name="bid" placeholder="Price">
        </div>
        <div class="form-group">
            <input class="form-control" type="image" name="photo" placeholder="Image">
        </div>
        <input class="btn btn-primary" type="submit" value="Post">
    </form>

{% endblock %}
'''
views.py
'''

    from django.contrib.auth import authenticate, login, logout
from django.db import IntegrityError
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse
from django.contrib.auth.decorators import login_required


from .models import User


def index(request):
    return render(request, "auctions/index.html")


def login_view(request):
    if request.method == "POST":

        # Attempt to sign user in
        username = request.POST["username"]
        password = request.POST["password"]
        user = authenticate(request, username=username, password=password)

        # Check if authentication successful
        if user is not None:
            login(request, user)
            return HttpResponseRedirect(reverse("index"))
        else:
            return render(request, "auctions/login.html", {
                "message": "Invalid username and/or password."
            })
    else: #if user didnt just fill form they need to go to login page to do that
        return render(request, "auctions/login.html")


def logout_view(request):
    logout(request)
    return HttpResponseRedirect(reverse("index"))


def register(request):
    if request.method == "POST": #if they just filled out register form
        username = request.POST["username"]
        email = request.POST["email"]

        # Ensure password matches confirmation
        password = request.POST["password"]
        confirmation = request.POST["confirmation"]
        if password != confirmation:
            return render(request, "auctions/register.html", {
                "message": "Passwords must match."
            })

        # Attempt to create new user
        try:
            user = User.objects.create_user(username, email, password)
            user.save()
        except IntegrityError:
            return render(request, "auctions/register.html", {
                "message": "Username already taken."
            })
        login(request, user) #logs in user 
        return HttpResponseRedirect(reverse("index")) #takes to index
    else:
        return render(request, "auctions/register.html") #if they click register

@login_required
def newListing(request):
    if request.method == "POST": #if they just enetered values for new listing
        title = request.POST["title"]
        description = request.POST["description"]
        bid = request.POST["bid"]
        image = request.POST["photo"]
    
    return render(request, "auctions/new.html") #right when they cllick the button

''' Rest of the form seems to work and i used the same format for the image.

question from:https://stackoverflow.com/questions/65896780/django-app-crashes-when-i-try-to-submit-a-photo-to-a-form

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...