Search code examples
pythondjangopythonanywhere

Django error only when deployed on PythonAnywhere: "No category matches the give query"


I have deployed My Django website via PythonAnywhere.com and everything seems to be working other than my search bar which gives me the the following error even if the item does exist. You can reproduce the error by going to https://www.ultimatecards5.com/ and searching for anything in the search bar. Although, when I run my project locally and do the same exact search I get the desired result of "0 results found" or a found result if it exists. Below are the error on the live page and then the same function when ran locally:

404 error when deployed

No 404 error when performing exact same thing locally

The error says raised by shops.views.allProductCat

My shops views.py

def allProdCat(request, c_slug=None): #used to show all products in that category
    c_page = None #for categories
    products_list = None
    if c_slug!=None:
        c_page = get_object_or_404(Category,slug=c_slug)
        products_list = Product.objects.filter(category=c_page,available=True) #filtering products according to category 
    else:
        products_list = Product.objects.all().filter(available=True)
    ''' Paginator Code '''
    paginator = Paginator(products_list,12) #limiting 6 products per category page
    try:
        page = int(request.GET.get('page','1')) #converting GET request to integer i.e page number 1 so we can store it in page variable
    except:
        page = 1
    try:
        products = paginator.page(page) 
    except (EmptyPage, InvalidPage):
        products = paginator.page(paginator.num_pages)
    return render(request,'shop/category.html',{'category':c_page,'products':products}) 

My Shops urls.py

from django.urls import path
from django.contrib import admin
from . import views

app_name='shop'

urlpatterns = [
    path('',views.allProdCat, name='allProdCat'),
    path('<slug:c_slug>/', views.allProdCat, name='products_by_category'), #view to show all products in a specific category
    path('<slug:c_slug>/<slug:product_slug>/', views.ProdCatDetail, name='ProdCatDetail'), #view for product details
]

My search app's views.py

from django.shortcuts import render
from shop.models import Product
from django.db.models import Q #importing Q objects to provide search functionality to the site

''' Keyword argument queries – in filter(), etc. – are “AND”ed together. If you need to execute more complex queries (for example, queries with OR statements), you can use Q objects.

A Q object (django.db.models.Q) is an object used to encapsulate a collection of keyword arguments. These keyword arguments are specified as in “Field lookups” above. '''

 
def searchResult(request):
    products = None
    query = None
    if 'q' in request.GET: #checking if there is a search request
        query = request.GET.get('q')
        products = Product.objects.all().filter(Q(name__contains=query) | Q(description__contains=query)) #searching products by queries
    return render(request,'search.html', {'query':query, 'products':products})

My Search App's urls.py

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

app_name = 'search_app'

urlpatterns = [
        re_path(r'^.*/', views.searchResult, name='searchResult'), #using re_path as path does'nt allow use of regular expressions and we are using regex here so django does'nt get confuse with patterns
]

Here is the Projects URLS.py

from django.contrib import admin
from django.urls import path, include
from shop import views
from django.conf import settings #importing settings 
from django.conf.urls.static import static #importing static after settings so we can map the static and media urls


urlpatterns = [

    path('admin/', admin.site.urls),

    path('cart/',include('cart.urls')),

    path('order/', include('order.urls')),

    path('account/create/', views.signupView, name = 'signup'),

    path('account/login/', views.signinView, name = 'signin'),

    path('account/logout/', views.signoutView, name = 'signout'),

    path('search/',include('search_app.urls')), 

    path('', include('shop.urls')), #Creating the patterns like this because if we put shop app first then django can get confused during the searching of products as it reads urlpatterns top to bottom which can lead to 404 page not found.

]


if settings.DEBUG: #mapping static and media url when debug is enabled 
    urlpatterns += static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)

I cannot figure out how to fix this on the live website.


Solution

  • As @Mugoma stated, my Search App's urls.py

    from . import views
    
    app_name = 'search_app'
    
    urlpatterns = [
            re_path(r'^.*/', views.searchResult, name='searchResult'), #using re_path as path does'nt allow use of regular expressions and we are using regex here so django does'nt get confuse with patterns
    ]
    

    I had one too many "/" and it should look like this instead:

    urlpatterns = [
            re_path(r'^.*', views.searchResult, name='searchResult'), #using re_path as path does'nt allow use of regular expressions and we are using regex here so django does'nt get confuse with patterns
    ]```