Quantcast
Channel: Active questions tagged html - Stack Overflow
Viewing all articles
Browse latest Browse all 72443

Django python View and Form - MultiValueDictKeyError at /result 'num1' - Why will my form not submit data

$
0
0

I've created a form to add two numbers and receive MultiValueDictKeyError at /result 'num1'. I've tried many things but can't figure it out. I figured I'd give this a try.

project url.py: """ from django.contrib import admin from django.urls import path, include from django.conf import settings from django.conf.urls.static import static from datetime import datetime

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('app1.urls')),
    path('result', include('app1.urls')),
]

urlpatterns = urlpatterns + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


App url.py:
from django.urls import path, include
from django.conf.urls import include, url
from django.conf import settings
from django.conf.urls.static import static
from . import views
from . import urls
from app1.models import Footprint
import app1.views


urlpatterns = [

    path("", views.index, name="index"),
    path('result', views.calc, name='result'),
]


views.py:
from django.shortcuts import render
from django.http import HttpResponse
from django.http import HttpRequest,HttpResponseRedirect
from .models import Footprint

# Create your views here.
def index(request):
    foots = Footprint.objects.all()         
    return render(request, "index.html", {'foots': foots})

def calc(request):
    val1 = int(request.POST['num1'])
    val2 = int(request.POST['num2'])
    res = val1 + val2
    return render(request,'result.html',{'result': res})


Form from index.html file:
block content %}
<form action="calc" method="post">
    {% csrf_token %}
    <p style="color:white;">Installation Cost:</p> <input type="text" name="num1">
    <p style="color:white;">Installation Year:</p> <input type="text" name="num2"><br><br>
    <p><a href="result" class="btn btn-primary btn-lg btn-custom">SUBMIT</a></p>

</form>
{% endblock %}



result.html:
{% block content %}
Result: {{result}}
{% endblock %}

Viewing all articles
Browse latest Browse all 72443

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>