姜戈模板错误;反转为未找到"搜索"。"搜索"不是有效的视图函数或模式名称



我的布局:

{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<title>{% block title %}{% endblock %}</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<link href="{% static 'encyclopedia/styles.css' %}" rel="stylesheet">
</head>
<body>
<div class="row">
<div class="sidebar col-lg-2 col-md-3">
<h2>Wiki</h2>
<form action = "{% url 'search' %}" method="GET">
<input class="search" type="text" name="q" placeholder="Search Encyclopedia">
</form>
<div>
<a href="{% url 'index' %}">Home</a>
</div>
<div>
<a href = "{% url 'newpage' %}">Create New Page</a>
</div>
<div>
<a>Random Page </a>
</div>
{% block nav %}
{% endblock %}
</div>
<div class="main col-lg-10 col-md-9">
{% block body %}
{% endblock %}
</div>
</div>
</body>
</html>

我的观点.py:

def search(request):   
result = set()
entries = util.list_entries()
if request.method == 'GET':
query = request.GET.get('q')    
if query == '': #If it's nothing
query = 'NONE'    
if query in entries:
return redirect(f'wiki/{query}')
else:
results = [entry for entry in entries if query.lower() in entry.lower()]
return render(request, "encyclopedia/index.html", {
"entries": results
})

return render(request, 'encyclopedia/search.html', {'results':result})
my urls:
from django.urls import path
from django.conf import settings
from django.conf.urls.static import static
from . import views
app_name = 'encyclopedia'
urlpatterns = [
path("", views.index, name="index"),
path('newpage/', views.new_page, name = 'newpage'),
path('wiki/<str:title>', views.entry, name = 'entries'),
path('wiki/<str:title>/edit',views.edit, name = 'edit'),
path('search', views.search, name = 'search'),
path('wiki/<str:entry>', views.random_page, name = 'random'),
]
urlpatterns += static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)

我该如何让它工作?它以前工作过,但现在停止工作了。URL中的名称与酒吧的名称匹配关于url方法我需要知道什么?我还尝试为随机页面编写{%url‘random‘%},但似乎根本不起作用当我把这个添加到一个随机搜索url停止工作,我不知道为什么

版本:Django 4.1.4,Python 3.10.5

您已经在urls.py文件中声明了app_name,这使得在django中调用url之前必须添加app_name。这样调用你的url。。。

href={% url 'app_name:url_name' %}

所以你的代码会是这样的。。。

{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<title>{% block title %}{% endblock %}</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<link href="{% static 'encyclopedia/styles.css' %}" rel="stylesheet">
</head>
<body>
<div class="row">
<div class="sidebar col-lg-2 col-md-3">
<h2>Wiki</h2>
<form action = "{% url 'encyclopedia:search' %}" method="GET">
<input class="search" type="text" name="q" placeholder="Search Encyclopedia">
</form>
<div>
<a href="{% url 'encyclopedia:index' %}">Home</a>
</div>
<div>
<a href = "{% url 'encyclopedia:newpage' %}">Create New Page</a>
</div>
<div>
<a>Random Page </a>
</div>
{% block nav %}
{% endblock %}
</div>
<div class="main col-lg-10 col-md-9">
{% block body %}
{% endblock %}
</div>
</div>
</body>
</html>

最新更新