Search code examples
pythonhtmldjangourlpycharm

Search results are not showing. And my URL Tab shows: "http://127.0.0.1:8000/search?" instead of this: "http://127.0.0.1:8000/search?q=name"


Codes on views.py

def searching(request):
    prod = None
    query = None
    if 'q' in request.GET:
        query = request.GET.get('q')
        prod = Products.objects.all().filter(Q(name__contains=query) | Q(des__contains=query))

    return render(request, 'search.html', {'qr': query, 'pr': prod})

Codes on urls.py

urlpatterns = [
    path('', views.home, name='hm'),
    path('<slug:c_slug>/', views.home, name='prod_cat'),
    path('<slug:c_slug>/<slug:product_slug>', views.prodetails, name='details'),
    path('search', views.searching, name='searching'),
]

Codes on search.html


 <div class="row flex px-xl-5">
{% for i in pr %}

            <div class="col-lg-3 col-md-4 col-sm-6">
                <div class="product-item bg-light mb-4">
                    <div class="product-img position-relative overflow-hidden">
                        <a href="{{i.get_urls}}"><img class="img-fluid w-100" src="{{i.img.url}}" alt="" style="height:250px; width:500px;">
                        </a>
                    </div>

                    <div class="text-center py-4">
                        <a class="h6 text-decoration-none text-truncate" href="{{i.get_url}}">{{i.name}}</a>
                        <p>{{i.des|truncatechars:80}}</p>
                        <div class="d-flex align-items-center justify-content-center mt-2">
                            <h5>RS {{i.price}}</h5><h6 class="text-muted ml-2"><del>RS 123.00</del></h6>
                        </div>
                        <p>Stocks left: {{i.stock}}</p>
                        <div class="d-flex align-items-center justify-content-center mb-1">
                            <small class="fa fa-star text-primary mr-1"></small>
                            <small class="fa fa-star text-primary mr-1"></small>
                            <small class="fa fa-star text-primary mr-1"></small>
                            <small class="fa fa-star text-primary mr-1"></small>
                            <small class="fa fa-star text-primary mr-1"></small>
                            <small>(99)</small>
                        </div>
                    </div>
                </div>
            </div>



{% endfor %}
     </div>

Codes on base.html

<div class="col-lg-4 col-6 text-left">
                <form action="{% url 'searching' %}" method="get">
                    <div class="input-group">
                        <input type="text" class="form-control" placeholder="Search for products">
                        <div class="input-group-append">
                            <span class="input-group-text bg-transparent text-primary">
                                <i class="fa fa-search"></i>
                            </span>
                        </div>
                    </div>
                </form>
            </div>

Actually, my problem is that when I search for a product, on the URL tab, it should be shown like this: "http://127.0.0.1:8000/search?q=name" and when I search, my URL tab shows only like this: "http://127.0.0.1:8000/search?" Please help me to fix this issue.

I need to fix this issue and please help me to fix this issue.


Solution

  • add a name attribute to your HTML input element like this

    <input type="text" class="form-control" placeholder="Search for products" name="q">