Reputation: 103
I am. trying to. build a search function for my django project base on the enter link description here
and the error message popped out "Reverse for 'Search' not found. 'Search' is not a valid view function or pattern name."
i have done the searching most of the advise asking me to check if the spelling is error
like my "search" in url or my search.html in my render
however, i have tried all the solution it still can't work
here is some of my code
urls.py:
from django.contrib import admin
from django.urls import path
from pages.views import home_view, contact_view, about_view
from products.views import product_detail_view, product_create_view, vendor_data,search_product
urlpatterns = [
path('', home_view, name='home'),
path('contact/', contact_view),
path('about/', about_view),
path('create/', product_create_view),
path('product/', product_detail_view),
path('search/', search_product),
path('vendor/', vendor_data),
path('admin/', admin.site.urls),
]
views.py
from django.shortcuts import render
from .models import Product
from .forms import ProductForm, RawProductForm,VendorForm
def search_product(request):
if request.method == "POST":
query_name = request.POST.get('title', None)
if query_name:
results = Product.objects.filter(name__contains=query_name)
return render(request, 'products/search.html', {"results":results})
return render(request, 'products/search.html')
search.html
<!DOCTYPE html>
<html>
<head>
<title>Django Search</title>
</head>
<body>
<form action="{% url 'search' %}" method="POST">
{% csrf_token %}
<input type="text" name="name">
<input type="submit" name="submit" value="Search">
</form>
{% for result in results %}
<p>{{result.name}}</p>
{% endfor %}
</body>
</html>
and following is my folder in case if needed
Upvotes: 0
Views: 6721
Reputation: 373
Encountering a similar issue while adding new features to my Django project during the testing phase
Reverse for 'detail' not found. 'detail' is not a valid view function or
pattern name.
I found out that when I call reverse("detail", kwargs={"pk": self.pk}) in my get_absolute_url() method, Django will look for a URL pattern with the name "detail" and substitute the value of self.pk into the URL pattern to generate the absolute URL for that particular instance of the Course model.
Without defining name='detail' in my urls.py, Django wouldn't have a named URL pattern to associate with the identifier "detail." Consequently, when attempting to use reverse("detail", kwargs={"pk": self.pk}) in my get_absolute_url() method, Django would be unable to locate the appropriate URL pattern, leading to the error message: "Reverse for 'detail' not found."
path('<int:pk>',views.CourseDetailView.as_view(),name='detail'),
Upvotes: 0
Reputation: 911
Change this file.. urls.py
from django.contrib import admin
from django.urls import path
from pages.views import home_view, contact_view, about_view
from products.views import product_detail_view, product_create_view, vendor_data,search_product
urlpatterns = [
path('', home_view, name='home'),
path('contact/', contact_view),
path('about/', about_view),
path('create/', product_create_view),
path('product/', product_detail_view),
path('search/', search_product, name = 'search'), # changed here
path('vendor/', vendor_data),
path('admin/', admin.site.urls),
]
Upvotes: 1
Reputation: 476493
You need to specify the name of the urls.py
with the name=…
parameter:
urlpatterns = [
# …,
# name ↓
path('search/', search_product, name='search'),
# …
]
Upvotes: 3