Home > Back-end >  Django MultiValueDictKeyError at /create/ 'howto-input'
Django MultiValueDictKeyError at /create/ 'howto-input'

Time:04-20

I'm trying to make simple form that would add a comment, I'm not using the forms.forms of django, I'm doing the forms from HTML directly and i want to use ajax to submit the form

my form simply has 1 file field and a publish button, everything seem finely coded but i get this MultiValueDictKeyError at /create/ 'howto-input'

views.py

def create(request):
    if request.method == "POST":
        content = request.POST['howto-input']
        user = request.POST['howto-user']
        topic = request.POST['howto-topic']
        new_question = AskQuestionOnHowToTopic(user=user, content=content, topic=topic)
        new_question.save()
        messages.success(request, f'Published')
        success = "Posted Successfully!"
        return HttpResponse(success)

def howtoTutorial(request, howtocat_slug, howto_slug):
        howtotut = HowToTutorial.objects.get(slug=howto_slug, howtocat=howtocat)
        context = {
            'howtotut': howtotut,
         }
        return render(request, 'howto/howto-tutorial.html', context)


urls.py

        path('create/', views.create, name="create")

template.html

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>


<form id="howto-form">
    {% csrf_token %}
    <input type="text" name="content" id="howto-input" />
    <input type="hidden" name="user" id="howto-user" value="{{ request.user }}" />
    <input type="hidden" name="topic" id="howto-topic" value="{{ howtotut }}" />
    <button  type="submit">Publish</button>
</form>


<script type="text/javascript">
    $(document).on("submit", "#howto-form", function (e) {
        e.preventDefault();
        $.ajax({
            type: "POST",
            // url: '/how-to/<howtocat_slug>/<howto_slug>/',
            url: "/create/",
            data: {
                content: $("#howto-input").val(),
                user: $("#howto-user").val(),
                topic: $("#howto-topic").val(),
                csrfmiddlewaretoken: $("input[name=csrfmiddlewaretoken]").val(),
            },
            success: function (data) {},
            error: function (xhr, ajaxOptions, thrownError) {
                alert(thrownError   "\r\n"   xhr.statusText   "\r\n"   xhr.responseText);
            },
        });
    });
</script>

enter image description here

CodePudding user response:

You use 'content', 'topic' and 'user' in ajax, when you create the request - but you are refering to the 'howto-user' and so on in the django view - this is probably whole problem - Django can't find the key in the dictionary - you can easily debug this in any given IDE - or just print out keys of the request.POST.

CodePudding user response:

In your create view you're searching for 'howto-input', 'howto-user' and 'howto-topic' keys in your POST request. But you're sending content, user and topic in your ajax payload. There's a key-value pair gap between your view and your POST request.

You can change the ajax payload or your view. (It seems easy to change your view)

 def create(request):
    if request.method == "POST":
        content = request.POST['content']
        user = request.POST['user']
        topic = request.POST['topic']
        ...
  • Related