如何在Django中正确处理获取html输入值到视图和url模式?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在Django中正确处理获取html输入值到视图和url模式?相关的知识,希望对你有一定的参考价值。
我无法弄清楚为什么在重定向到试图访问html文件中的输入数据到pages.py的页面时,我得到一个MultiValueDictKeyError。看来该视图在我的html文件中找不到“main_search”。基本上我要做的是从html文件中的输入中获取值,并将其用于url模式,并在views.py中显示在该页面上。我觉得这是一件非常明显的事情,但我没有把握这个问题。
这是错误:
追溯:
getitem 77中的文件“C:\ Users \ stovi \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ django \ utils \ datastructures.py”。list_ = super()。getitem(key)
在处理上述异常('main_search')期间,发生了另一个异常:
内部34中的文件“C:\ Users \ stovi \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ django \ core \ handlers \ exception.py”response = get_response(request)
_get_response 126中的文件“C:\ Users \ stovi \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ django \ core \ handlers \ base.py”.response = self.process_exception_by_middleware(e,request )
_get_response 124中的文件“C:\ Users \ stovi \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ django \ core \ handlers \ base.py”.response = wrapped_callback(request,* callback_args, ** callback_kwargs)
在search_page 31中搜索文件“C:\ Users \ stovi \ Desktop \ simple_search \ simple_search_site \ main \ views.py”31. search = request.POST [“main_search”]
getitem 79中的文件“C:\ Users \ stovi \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ django \ utils \ datastructures.py”。raise MultiValueDictKeyError(key)
异常类型:/ search = testing / Exception值中的MultiValueDictKeyError:'main_search'
views.朋友:
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.朋友:
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>
任何帮助,将不胜感激。另外,我在网络开发或Django方面的经验很少,所以如果有些事情我做错了或不正确,那就继续烤我吧。
替换:
search = request.POST["main_search"]
至
search = request.POST.get("main_search","");
以上是关于如何在Django中正确处理获取html输入值到视图和url模式?的主要内容,如果未能解决你的问题,请参考以下文章