I can not figure out why I'm getting a MultiValueDictKeyError when redirecting to a page trying to access data from an input in an html file to views.py. It appears that the view cannot find "main_search" in my html file. Basically what I'm trying to do is get the value from an input in an html file and use it for the url pattern and in views.py to display on that page. I have a feeling it's something painfully obvious, but I've had no luck figuring out the issue.
Here is the error:
Traceback:
File "C:\Users\stovi\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\utils\datastructures.py" in getitem 77. list_ = super().getitem(key)
During handling of the above exception ('main_search'), another exception occurred:
File "C:\Users\stovi\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\exception.py" in inner 34. response = get_response(request)
File "C:\Users\stovi\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\base.py" in _get_response 126. response = self.process_exception_by_middleware(e, request)
File "C:\Users\stovi\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\base.py" in _get_response 124. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\stovi\Desktop\simple_search\simple_search_site\main\views.py" in search_page 31. search = request.POST["main_search"]
File "C:\Users\stovi\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\utils\datastructures.py" in getitem 79. raise MultiValueDictKeyError(key)
Exception Type: MultiValueDictKeyError at /search=testing/ Exception Value: 'main_search'
views.py:
from django.shortcuts import render
from django.http import HttpResponse
import wikipedia
def homepage(request):
return render(request=request,template_name="main/search.html")
def wiki_page(request):
return render(request=request,template_name="main/wikis.html")
def map_page(request):
return render(request=request,template_name="main/maps.html")
def video_page(request):
return render(request=request,template_name="main/videos.html")
def book_page(request):
return render(request=request,template_name="main/books.html")
def game_page(request):
return render(request=request,template_name="main/games.html")
def movie_page(request):
return render(request=request,template_name="main/movies.html")
def about_page(request):
return render(request=request,template_name="main/about.html")
def search_page(request,search):
search = request.POST["main_search"]
return HttpResponse(search)
urls.py:
from django.urls import path, include
from . import views
app_name = 'main'
urlpatterns = [
path('', views.homepage, name='homepage'),
path('search=<str:search>/',views.search_page,name='searchpage'),
path('wikis/',views.wiki_page,name='wikipage'),
path('maps/',views.map_page,name='mappage'),
path('videos/',views.video_page,name='videopage'),
path('books/',views.book_page,name='bookpage'),
path('games/',views.game_page,name='gamepage'),
path('movies/',views.movie_page,name='moviepage'),
path('about/',views.about_page,name='aboutpage'),
]
search.html
<html>
{% extends "main/navbar.html" %}
{% block content %}
<main>
<div class="container">
<div style="margin-top:15%">
<h1 class="green-text center-align text-darken-4">Simple Search</h1>
</div>
<form action='"/search="+document.getElementById("main_s").value' name="search" method="POST">
<div class="row">
<div class="input-field">
<input class="green-text text-darken-4 z-depth-0" style="border-color: forestgreen" type="text" id="main_s" name="main_search"/>
</div>
</div>
<div class="center-align">
<a class="waves-effect waves-light btn green darken-4" id="wikis"href="" onclick="this.href='/search='+document.getElementById('main_s').value">Wiki Search</a>
<a class="waves-effect waves-light btn green darken-4" id="maps">Map Search</a>
<a class="waves-effect waves-light btn green darken-4">Video Search</a>
<a class="waves-effect waves-light btn green darken-4">Book Search</a>
<a class="waves-effect waves-light btn green darken-4">Game Search</a>
<a class="waves-effect waves-light btn green darken-4">Movie Search</a>
</div>
</form>
</div>
</main>
{% endblock %}
</html>
Any help would be appreciated. Also, I have little experience in web development or Django so if there are things I'm doing wrong or improperly then go ahead and roast me.