'local variable 'search_keyword' referenced before assignment
I have been working on a search form and it's giving me this error when i open the url or submit the form. I am watching a codemy django tutorial and the only thing that is different is that i have some code for pagination i don't know if that's what is affecting it.
base.html
<div class="header-search-wrapper novis_search">
<form method="POST" action="{% url 'listing' %}" name="searchform">
{% csrf_token %}
<div class="custom-form">
<label>Keywords </label>
<input type="text" placeholder="Property Keywords" name="search"/>
<!-- <label >Categories</label>
<select data-placeholder="Categories" name = "home_type" class="chosen-select on-radius no-search-select" >
<option>All Categories</option>
<option>Single-family</option>
<option>Semi-detached</option>
<option>Apartment</option>
<option>Townhomes</option>
<option>Multi-family</option>
<option>Mobile/Manufactured</option>
<option>Condo</option>
</select>
<label style="margin-top:10px;" >Price Range</label>
<div class="price-rage-item fl-wrap">
<input type="text" class="price-range" data-min="10000" data-max="100000000000" name="price-range1" data-step="1" value="1" data-prefix="$₦">
</div> -->
<button type="submit" class="btn float-btn color-bg"><i class="fal fa-search"></i> Search</button>
</div>
</form>
</div>
views.py
def listing(request):
if request.method == 'POST' and 'searchform' in request.POST :
search = request.POST['search']
print(search)
propertys = Property.objects.filter(name__icontains=search)
p = Paginator(Property.objects.order_by('-listed_on'), 2)
page = request.GET.get('page')
propertys = p.get_page(page)
nums = "p" * propertys.paginator.num_pages
return render(request, 'listing.html',{'search_keyword':search,'nums':nums,'propertys':propertys})
else:
p = Paginator(Property.objects.order_by('-listed_on'), 2)
page = request.GET.get('page')
propertys = p.get_page(page)
nums = "p" * propertys.paginator.num_pages
return render(request, 'listing.html',{'propertys':propertys, 'nums':nums})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|