仅当部署在 PythonAnywhere 上时出现 Django 错误:"No category matches the give query"
Django error only when deployed on PythonAnywhere: "No category matches the give query"
我已经通过 PythonAnywhere.com 部署了我的 Django 网站,除了我的搜索栏之外,一切似乎都在工作,即使该项目确实存在,它也会给我以下错误。您可以通过转至 https://www.ultimatecards5.com/ 并在搜索栏中搜索任何内容来重现该错误。虽然,当我在本地 运行 我的项目并进行相同的精确搜索时,我得到了“找到 0 个结果”或找到的结果(如果存在)的期望结果。以下是实时页面上的错误,然后是 运行 本地时的相同功能:
错误说 shops.views.allProductCat
我的商店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})
我的商店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
]
我的搜索应用 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})
我的搜索应用 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
]
这是项目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)
我不知道如何在实时网站上解决这个问题。
正如@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
]
我的“/”太多了,它应该看起来像这样:
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
]```
我已经通过 PythonAnywhere.com 部署了我的 Django 网站,除了我的搜索栏之外,一切似乎都在工作,即使该项目确实存在,它也会给我以下错误。您可以通过转至 https://www.ultimatecards5.com/ 并在搜索栏中搜索任何内容来重现该错误。虽然,当我在本地 运行 我的项目并进行相同的精确搜索时,我得到了“找到 0 个结果”或找到的结果(如果存在)的期望结果。以下是实时页面上的错误,然后是 运行 本地时的相同功能:
错误说 shops.views.allProductCat
我的商店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})
我的商店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
]
我的搜索应用 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})
我的搜索应用 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
]
这是项目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)
我不知道如何在实时网站上解决这个问题。
正如@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
]
我的“/”太多了,它应该看起来像这样:
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
]```