def search(request): form = SearchForm(request.POST) try: loggeduser = User.objects.get(id=request.session['USERZ']) except (KeyError, User.DoesNotExist): loggeduser = None all_posts = Posts.objects.all() if form.is_valid(): queryTag = form.save(commit=False) query = form.cleaned_data["tag"] return render(request, 'marketplace/search.html', {'all_posts':all_posts, 'query':query,'loggeduser':loggeduser, 'form':form}) return render(request, 'marketplace/search.html', {'loggeduser':loggeduser,'form':form})
def search_results(request): form = SearchForm(request.GET) if form.is_valid(): query = form.cleaned_data['q'] else: return render(request, 'marketplace/index.html', {'form':form}) # query = '' results = [] try: query = request.GET['q'] if len(query) > 0: results = Item.objects.filter(Q(title__contains=query) | Q(description__contains=query)) except (NameError, KeyError): return render(request, 'marketplace/search_results.html', {'query':query, 'resuts':results, 'error_message': "No results found",}) else: return render(request, 'marketplace/search_results.html', {'query':query, 'results':results})
def itemdetails2(request,post_id,offer_id): all_posts = Posts.objects.all() all_offers = Offer.objects.all() posts = Posts.objects.get(pk=post_id) offer = Offer.objects.get(pk=offer_id) off_type = offer.OfferType checker = 0 try: user = User.objects.get(id=request.session['USERZ']) except (KeyError, User.DoesNotExist): user = None form = SearchForm(request.POST) queryTag = form.save(commit=False) query = form.cleaned_data["tag"] if request.POST.get("Update"): checker = 1 if off_type == 'Buy': form = OfferBuyForm(request.POST or None, request.FILES or None) elif off_type == 'Swap': form = OfferSwapForm(request.POST or None, request.FILES or None) form.fields['offer_post'].queryset = Posts.objects.filter(user = user) if form.is_valid(): offer = form.save(commit=False) offer.save(); return render(request,'marketplace/viewitem2.html',{'all_posts':all_posts,'posts':posts, 'loggeduser':user, 'form':form,'all_offers':all_offers,'offer':offer}) if request.POST.get("Cancel"): offer.delete() return render(request,'marketplace/home.html',{'all_posts':all_posts, 'loggeduser':user, 'form':form,'all_offers':all_offers}) if query != '': return render(request, 'marketplace/search.html', {'all_posts':all_posts, 'query':query,'loggeduser':user, 'form':form}) return render(request,'marketplace/viewitem2.html',{'all_posts':all_posts,'posts':posts, 'loggeduser':user, 'form':form,'all_offers':all_offers,'offer':offer})