Simple Search Feature

20 views
Skip to first unread message

tango ward

unread,
Feb 24, 2018, 8:18:25 PM2/24/18
to django...@googlegroups.com
Hi,

I am playing around on adding a search feature to a website. I am currently encountering a problem when the page should load a list of songs and after typing in a song title in the search box:


views.py

class SongListView(ListView):
    model = Song
    context_object_name = 'songs'
    template_name = 'songs/song_list.html'

    def get(self, request, *args, **kwargs):

        if request.method == 'GET':
            song_name = request.GET.get('name', "Error")
            songs = self.model.objects.filter(name__icontains=song_name)
        else:
            songs = self.models.all()
        return render(request, self.template_name, {'songs': songs})

problem is when I click the search button with the text box empty, I am getting the list of all the song (url: /songs/?name=) but if I just load the page wihout clicking the submit button (url: /songs/), it doesn't give me the list all the songs. The search box works if I type the correct song name as it shows the song title.  Problem is the page should load all the songs before I search a particular song.

Any suggestions so I can enhance my code?


Thanks,
Jarvis

Ken Whitesell

unread,
Feb 24, 2018, 9:21:46 PM2/24/18
to Django users
One of the issues is here:
        if request.method == 'GET':
            song_name = request.GET.get('name', "Error")
            songs = self.model.objects.filter(name__icontains=song_name)
        else:
            songs = self.models.all()
        return render(request, self.template_name, {'songs': songs})

When no parameter is passed, request.method is still "GET" - In fact, this method should only be called on a get method, making this test irrelevant.

You _could_ change it to look something like this:
        song_name = request.GET.get('name', None)
        if song_name:

            songs = self.model.objects.filter(name__icontains=song_name)
        else:
            songs = self.models.all()
        return render(request, self.template_name, {'songs': songs})

Hope this helps,
     Ken


On Saturday, February 24, 2018 at 8:18:25 PM UTC-5, tangoward15 wrote:
Hi,

I am playing around on adding a search feature to a website. I am currently encountering a problem when the page should load a list of songs and after typing in a song title in the search box:


views.py

class SongListView(ListView):
    model = SongOne 

    context_object_name = 'songs'
    template_name = 'songs/song_list.html'

    def get(self, request, *args, **kwargs):

        if request.method == 'GET':
            song_name = request.GET.get('name', "Error")
            songs = self.model.objects.filter(name__icontains=song_name)
        else:
            songs = self.models.all()
        return render(request, self.template_name, {'songs': songs})

tango ward

unread,
Feb 25, 2018, 1:11:15 AM2/25/18
to django...@googlegroups.com
thanks for the suggestions Ken. I just want to ask too if it's safe to display the list of songs even if the textbox is empty?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/fee7baf6-7dbe-4a33-a281-f7c8600f73cc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jani Tiainen

unread,
Feb 25, 2018, 8:26:50 AM2/25/18
to django...@googlegroups.com
Hi,

"Is it safe"? Well that is your call to make. Depending number of songs it might not be meanful for enduser to see whole list which leads to things like pagination and restricting maximum number of returned entries.


For more options, visit https://groups.google.com/d/optout.



--
Jani Tiainen

- Well planned is half done, and a half done has been sufficient before...

José Sánchez Moreno

unread,
Feb 25, 2018, 10:26:29 AM2/25/18
to tango ward, django...@googlegroups.com
I would do something simitar to:

class SongListView(ListView):
model = Song
context_object_name = 'songs'
template_name = 'songs/song_list.html'

def get_queryset(self):
qs = super().get_queryset()
name = self.request.get("name")
if name:
qs = qs.filter(name__icontains=name)
return qs

El dom, feb 25 2018 a las 09:17:07 , tango ward <tango...@gmail.com>
escribió:
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAA6wQLJ5bnMzR1etSRY191KL3fWkMpPsvhKnFBKtAuc1iLNKFg%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.

--
José Sánchez Moreno <jo...@o2w.es>
O2W Leading Software - www.o2w.es
C/ Trovero Marin, 5 30730 San Javier (Murcia)
Tlf: 968 192 905 - 656 817 548
Condiciones Legales de este e-mail en: http://www.o2w.es/email
Reply all
Reply to author
Forward
0 new messages