未找到“”的Django render()Reverse。“”不是有效的视图函数或模式名称

2024-05-18 04:28:03 发布

您现在位置:Python中文网/ 问答频道 /正文

这是另一个涉及Django路径的问题。我在任何地方都找不到我的答案,为此我做了大量的搜索。在

在我的视图中,return()函数抛出错误

django.urls.exceptions.NoReverseMatch: Reverse for '' not found. '' is not a valid view function or pattern name.

这是我的密码。在

<!-- siren_search.html -->
    <div class="row">
        <div class="col-sm-8 col-md-7 col-xl-5 mx-auto">
            <form id="searchform" action="{% url 'search' %}" method="GET">
                <input id="searchbar" name="query" autocomplete="on" onkeyup=getCameras(this.value)
                    placeholder="Search for the name of a jobsite." class="form-control" type="search" />
            </form>
        </div>
    </div>
^{pr2}$

#### views.py

from django.shortcuts import render
from django.http import HttpResponse, JsonResponse
from django.core.exceptions import ObjectDoesNotExist
from .models import CameraSystem, CameraModel, ControlByWeb, JobSite
from django.core import serializers
import json

def siren_home(request):

    # some functionality

    return render(request, 'siren_search.html', context)

def search(request):

    term = request.GET.get('query')
    context = {}

    # Handle when the user presses enter on the search bar
    if 'query' in request.GET and term != '' and not request.is_ajax():
        try:
            jobsite = JobSite.objects.get(name__iexact = term)
            cameras = jobsite.camerasystem_set.all()
            context = {
                'cameras': cameras,
            }

        except ObjectDoesNotExist:
            pass

        return render(request, 'siren_search.html', context) # Django fails here
    else:
        return render(request, 'siren_search.html', context)

当我点击搜索栏上的enter时,它将路由到正确的view函数并执行所有必要的计算,但是在render()函数上失败了。我在浏览器中的url是:http://localhost:8000/siren-search/search/?query=jobsite9。在

以下是我的回溯链接:http://dpaste.com/2KFAW9M#


Tags: django函数namefromimportdivsearchreturn
2条回答

真的只值得一个评论,但我不能在那里格式。在

在你链接的回溯中

Template error:
In template /Users/name/Programming/test/webapp/webapp/templates/base.html, error at line 37
   Reverse for '' not found. '' is not a valid view function or pattern name.

这对我来说意味着你可能找错地方了,事实上问题甚至可能不在你所写的代码中。请看第37行基本.html,并查看它是否依赖于上下文中未传递的内容。或者试着把它拔出来基本.html现在。在

我很困惑,因为海妖_搜索.htmlas post没有显示它正在扩展任何基本模板。在

试着在模板中给出这样的语法

             **"{% url 'appname:search' %}"**

这可能有用

相关问题 更多 >