简体   繁体   English

仅在 PythonAnywhere 上部署时出现 Django 错误:“没有类别与给定查询匹配”

[英]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.我已经通过 PythonAnywhere.com 部署了我的 Django 网站,除了我的搜索栏之外,一切似乎都在工作,即使该项目确实存在,它也会给我以下错误。 You can reproduce the error by going to https://www.ultimatecards5.com/ and searching for anything in the search bar.您可以通过转到https://www.ultimatecards5.com/并在搜索栏中搜索任何内容来重现该错误。 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.虽然,当我在本地运行我的项目并执行相同的精确搜索时,我会得到“找到 0 个结果”的期望结果或找到的结果(如果存在)。 Below are the error on the live page and then the same function when ran locally:以下是实时页面上的错误,然后是在本地运行时相同的 function:

部署时出现 404 错误

在本地执行完全相同的操作时没有 404 错误

The error says raised by shops.views.allProductCat该错误说是由 stores.views.allProductCat 引发的

My shops views.py我的商店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我的商店 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我的搜索应用的 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我的搜索应用程序的 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这是项目 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.我无法弄清楚如何在实时网站上解决此问题。

As @Mugoma stated, my Search App's urls.py正如@Mugoma 所说,我的搜索应用程序的 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
]```

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM