为什么我的else子句在Django中不起作用?

oxf4rvwz  于 2022-12-05  发布在  Go
关注(0)|答案(2)|浏览(153)

我正在尝试为我的电子商务网站创建一个搜索错误。当用户输入一个不在数据库中的搜索时,它应该返回搜索错误页面。尽管看起来我的else子句不起作用。
我尝试在search.html页面中放置else子句,但是它总是给我错误,并且当我尝试修复错误时,似乎什么也没有发生,它保持不变。我期望search_error. html页面在用户输入数据库中没有的产品名称时出现。尽管我不断得到例如,当我键入“hello”时,“页面显示“Search results for hello”。但结果应该是search_error.html页面。我目前还在我的www.example.com中尝试了else子句views.py,但它显示了同样的结果。我想我的else子句不起作用,我不知道为什么。
我的views.py:

def search(request):
    if 'searched' in request.GET:
        searched = request.GET['searched']
        products = Product.objects.filter(title__icontains=searched)
        return render(request, 'epharmacyweb/search.html', {'searched': searched, 'products': products})
    else:
        return render(request, 'epharmacyweb/search_error.html')

def search_error(request):
    return render(request, 'epharmacyweb/search_error.html')

我的urls.py位于URL模式下:

path('search/', views.search, name='search'),
path('search_error/', views.search_error, name='search_error'),

我的search.html页面:

{% if searched %}
            <div class="pb-3 h3">Search Results for {{ searched }}</div>
              <div class="row row-cols-1 row-cols-sm-2 row-cols-md-5 g-3">
                {% for product in products %}
                    <div class="col">
                        <div class="card shadow-sm">
                          <img class="img-fluid" alt="Responsive image" src="{{ product.image.url }}">
                          <div class="card-body">
                            <p class="card-text">
                              <a class="text-dark text-decoration-none" href="{{ product.get_absolute_url }}">{{ product.title }}</a>
                            </p>
                            <div class="d-flex justify-content-between align-items-center">
                              <small class="text-muted"></small>
                            </div>
                          </div>
                        </div>
                    </div>
                    {% endfor %}
              </div>
                <br></br>

        {% else %}
            <h1>You haven't searched anything yet...</h1>
        {% endif %}
chhqkbe1

chhqkbe11#

if返回true是因为在request.GET字典中,术语'searched' * 实际上是 *,这并不意味着数据库中存在值为request.GET['searched']的产品,当您键入“hello”时可能是“hello”。request.GET是一个字典,其 key'searched',值为“hello”。您也可以使用get(),它会取得request['searched']的值或传回None,因此您根本不需要使用if来检查它。
现在要检查数据库中是否有搜索词的值,可以检查查询集:

def search(request):
    
    # Return the value or None
    searched = request.GET.get('searched')

    products = Product.objects.filter(title__icontains=searched)

    # Check if there are any products with the search term:
    if products:
        return render(request, 'epharmacyweb/search.html', {'searched': searched, 'products': products})
    else:
        return render(request, 'epharmacyweb/search_error.html')
yrdbyhpb

yrdbyhpb2#

我认为您需要检查products = Product.objects.filter(title__icontains=searched)是否返回结果,而不是检查GET参数中是否包含“searched”。
要检查数据库是否返回结果,您可以exists()
https://docs.djangoproject.com/en/4.1/ref/models/querysets/#django.db.models.query.QuerySet.exists

相关问题