【问题标题】:MultiValueDictKeyError at /search/, Why am I getting this error?/search/ 处的 MultiValueDictKeyError,为什么会出现此错误?
【发布时间】:2020-07-17 14:14:03
【问题描述】:

我是django的初学者,遇到这个错误MultiValueDictKeyError,我之前遇到过这个错误,我在将名称标签添加到HTML文件后解决了它,但这次我无法弄清楚。请帮我解决这个错误

views.py

from django.shortcuts import render
from .models import Post

# Create your views here.
def main(request):
    return render(request, "blog/index.html", {"Posts":Post.objects.all()})

def viewpost(request, pk):   
    return render(request, "blog/viewpost.html", {"Posts":Post.objects.get(id = pk)})

def search(request):
    if request.method == "GET":
        search = request.GET["search"]
    for post in Post.objects.all():
        if search in post.Title:
            return render(request, "blog/search.html")
        else:
            return render(request, "blog/search.html", {
                "message": "Not Found"
            })

Index.html 很抱歉,这个文件太大了,但它位于第三块。我已经注释掉了那个块。

{% extends 'blog/layout.html' %}
{% load static %}

{% block body %}
    

    

    <!-- Page Content -->
    <div class="container">

        <div class="row">

        <!-- Blog Entries Column -->
        <div class="col-md-8">

            <h1 class="my-4">Page Heading
            <small>Secondary Text</small>
            </h1>

            <!-- Blog Post -->
            {% for post in Posts %}
                <div class="card mb-4">
                <img class="card-img-top" src="http://placehold.it/750x300" alt="Card image cap">
                <div class="card-body">
                    <h2 class="card-title">{{ post.Title }}</h2>
                    <p class="card-text">{{ post.Description }}</p>
                    <a href="#" class="btn btn-primary">Read More &rarr;</a>
                </div>
                <div class="card-footer text-muted">
                    Posted on {{ post.Date }} by
                    <a href="#">{{ post.Author }}</a>
                </div>
                </div>
            {% endfor %}
            
            <!-- Pagination -->
            <ul class="pagination justify-content-center mb-4">
            <li class="page-item">
                <a class="page-link" href="#">&larr; Older</a>
            </li>
            <li class="page-item disabled">
                <a class="page-link" href="#">Newer &rarr;</a>
            </li>
            </ul>

        </div>

        <!-- Sidebar Widgets Column -->
        <div class="col-md-4">

            **<!-- Search Widget -->
            <div class="card my-4">
            <h5 class="card-header">Search</h5>
            <div class="card-body">
                <form action="{% url 'search' %}" method="GET">
                {% csrf_token %}
                    <div class="input-group">
                    <input type="text" class="form-control" placeholder="Search for..." name="search">
                    <span class="input-group-append">
                        <button class="btn btn-secondary" type="button" ><a href="{% url 'search' %}">Go!</a></button>
                    </span>
                    </div>
                </form>**
            </div>
            </div>

            <!-- Categories Widget -->
            <div class="card my-4">
            <h5 class="card-header">Categories</h5>
            <div class="card-body">
                <div class="row">
                <div class="col-lg-6">
                    <ul class="list-unstyled mb-0">
                    <li>
                        <a href="#">Web Design</a>
                    </li>
                    <li>
                        <a href="#">HTML</a>
                    </li>
                    <li>
                        <a href="#">Freebies</a>
                    </li>
                    </ul>
                </div>
                <div class="col-lg-6">
                    <ul class="list-unstyled mb-0">
                    <li>
                        <a href="#">JavaScript</a>
                    </li>
                    <li>
                        <a href="#">CSS</a>
                    </li>
                    <li>
                        <a href="#">Tutorials</a>
                    </li>
                    </ul>
                </div>
                </div>
            </div>
            </div>

            <!-- Side Widget -->
            <div class="card my-4">
            <h5 class="card-header">Side Widget</h5>
            <div class="card-body">
                You can put anything you want inside of these side widgets. They are easy to use, and feature the new Bootstrap 4 card containers!
            </div>
            </div>

        </div>

        </div>
        <!-- /.row -->

    </div>
    <!-- /.container -->

    
    
    
{% endblock %}

urls.py

from django.urls import path, include
from . import views


urlpatterns = [
    path('',views.main, name = 'main'),
    path('viewpost/<int:pk>/', views.viewpost, name = 'viewpost'),
    path('search/', views.search, name = 'search'),
    
]

搜索.html

{% extends 'blog/layout.html' %}
{% load static %}

{% block body %}
    <div class="card" style="width: 70vw; margin-left: auto; margin-right: auto; margin-top: 20px; ;">
        <div class="card-body">
        <h2 class="card-title">Search Results:</h2> 
        {% for post in posts %}      
            <h3 class="card-subtitle" style="margin-top: 30px;"><a href="#}" >{{ post.title }}</a></h3>
            <p class="card-text" style="margin-left: 1px;">{{ post.description }}</p>
            <hr>
        {% endfor %}
        </div>
    </div>
    
{% endblock %}

【问题讨论】:

    标签: html css python-3.x django


    【解决方案1】:

    您需要像这样更改视图和模板。

    将按钮类型更改为submit,GET 请求中不需要 csrf 令牌。

      <form action="{% url 'search' %}" method="GET">
      <div class="input-group">
      <input type="text" class="form-control" placeholder="Search for..." name="search">
      <span class="input-group-append">
          <button class="btn btn-secondary" type="submit">Go</button>
      </span>
      </div>
      </form>**
                
    

    现在在视图中

    首先获取搜索查询参数,然后使用__icontains 过滤包含该查询的所有帖子,并将结果返回到上下文中的模板

    def search(request): 
        search = request.GET.get ('search', '')
        posts = Post.objects.filter(title__icontains=search)
        return render(request, "blog/search.html", {'posts':posts})
          
                
    

    【讨论】:

    • 但是什么时候在html中使用posts.Title,它什么也没显示,我认为搜索等于None,我已经更新了HTML文件
    • 看看print (search)会给出什么结果。
    • 它返回无,但没关系,我已经修好了。问题出在按钮上。他们的锚标签造成了问题,我删除了它,一切都运行良好。但是为什么会这样呢?
    猜你喜欢
    • 2021-08-04
    • 2021-03-29
    • 2022-01-04
    • 2018-12-06
    • 1970-01-01
    • 2021-01-08
    • 2021-09-03
    • 1970-01-01
    • 2017-04-24
    相关资源
    最近更新 更多