Gornl
Gornl

Reputation: 11

Django searching function issue

I tried implement searching function in my app , but I've got the error it's call typeError .I add the picture of this error

views.py

from django.shortcuts import render
from .models import Games,Game_detail,Order
from django.shortcuts import render,get_object_or_404,redirect
from django.views.generic import ListView,DetailView,CreateView
from django.db.models import Q
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

class IndexView(ListView):
    template_name ='games/list.html'
    context_object_name = 'all_games'
    model = Games

    def get_queryset(self,request):
        query = request.GET.get("q")
        if query:
                queryset_list = queryset_list.filter(
                Q(name__icontains=query)|
                Q(platform__icontains=query)|
                Q(genre__icontains=query) |
                Q(language__icontains=query)
                ).distinct()
        return Games.objects.all()

list.html

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity=" sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
 <title>Page Title</title>
 </head>
 <body>
</body>
</html>
<form method='GET' action=''>
<input type='text' name='q' placeholder='Wyszukaj' value='{{ request.GET.q }}'/>
<input type='submit'class="btn btn-default" value='Szukaj' />
</form>
<table class="table table-striped">
 <thead>
   <tr>
        <th>Nazwa</th>
        </th></th>
          <th>Platforma</th>
            <th>Gatunek</th>
            <th>Język</th>
        </tr>
    </thead>
<tbody>
{% for obj in object_list%}
<tr>
<td><a href ='{%url "detail" pk=obj.pk%}'>{{obj.name}}</a></td>
<td>{{obj.platform}}</td>
<td>{{obj.genre}}</td>
<td>{{obj.language}}</td>
</tr>
{% endfor %}
</tbody>
</table>

Model.py

from django.db import models
class Games(models.Model):
    name = models.CharField(max_length=250)
    platform = models.CharField(max_length=500)
    genre = models.CharField(max_length=100)
    language = models.CharField(max_length=100)
    image = models.ImageField(upload_to='Images_game',default='' )
  # def __str__ (self):
   # return self.Name

    class Game_detail(models.Model):
        game = models.ForeignKey(Games,on_delete=models.CASCADE)
        subcryption = models.TextField()
        publisher = models.CharField(max_length=250)
        date_of_publish = models.DateField()

    class Order(models.Model):
        person_name = models.CharField(max_length=24)
        person_surname = models.CharField(max_length=24)
        street = models.CharField(max_length=45)
        city= models.CharField(max_length=45)

Error issue

I want write searching for this 4 values . I guess problem depend on return function could be wrong.

Upvotes: 0

Views: 44

Answers (1)

taras
taras

Reputation: 3705

You have a wrong signature of the get_queryset method. It does not accept request as a second parameter. You can access request from a self variable.

class IndexView(ListView):
    template_name = 'games/list.html'
    context_object_name = 'all_games'
    model = Games

    def get_queryset(self):  # no second parameter
        query = self.request.GET.get("q")  # get request here
        queryset_list = Games.objects.all()  # initialize queryset
        if query:
            # apply filter to the queryset_list
            queryset_list = queryset_list.filter(
                Q(name__icontains=query) |
                Q(platform__icontains=query) |
                Q(genre__icontains=query) |
                Q(language__icontains=query)
            ).distinct()
        return queryset_list

You can find more information in django documentation of get_queryset method

Upvotes: 1

Related Questions