Create A Search Bar - Django Wednesdays #9

Поделиться
HTML-код
  • Опубликовано: 30 янв 2025

Комментарии • 147

  • @Codemycom
    @Codemycom  3 года назад +4

    ▶️ Watch Entire Django Wednesdays Playlist ✅ Subscribe To My RUclips Channel:
    bit.ly/35Xo9jD bit.ly/2IGzvOR
    ▶️ See More At: ✅ Join My Facebook Group:
    Codemy.com bit.ly/2GFmOBz
    ▶️ Learn to Code at Codemy.com ✅ Buy a Codemy T-Shirt!
    Take $30 off with coupon code: youtube1 bit.ly/2VC9WUN
    ▶️ Get The Code
    bit.ly/3sJpeV6

    • @ahgpy
      @ahgpy 3 года назад

      man ur awesome
      this is not clickable Read more...

  • @PatrickAmone
    @PatrickAmone 10 месяцев назад +1

    Actually this is my first comment in RUclips. I am a software developer in other languages, so I embarked to learn django, which i have been reading documentation, but when i started following your videos, I can proudly say that you deserve award. Your lecturer to say, is straight forward and on point. Good job.

    • @Codemycom
      @Codemycom  10 месяцев назад

      Thanks! Glad you enjoy the videos!

  • @stachcode9085
    @stachcode9085 3 года назад +1

    there are a lot of tutorials in youtube but the secret key to understand a topic is the instructor style. i started to learn basics of django from you and know i started to make some mony from coding and when i get stuck in something i always come back to you videos and i always find the solution. learning from u is alwyas easy and fun thanks man. god bless you

  • @johnsolly
    @johnsolly 3 года назад +33

    If you want to search across multiple fields in the post/venue model (Like the content or the title), you can do it like this:
    from django.db.models import Q
    filtered_posts = Post.objects.filter(Q(content__contains=searched) | Q(title__contains=searched))
    The pipe character, '|' means 'OR' so you can search across both the content and the tile at the same time!

    • @amangautam1779
      @amangautam1779 2 года назад

      What is Q here?

    • @horger89
      @horger89 2 года назад +1

      @@amangautam1779 stands for query if I'm not mistaken

    • @Charoiz
      @Charoiz 2 года назад

      And if someone needs to search through Foreignkeys for example for events, you would add
      Filter(Q(Venue__address__contains= searched)

    • @VladyslavHoncharov-c6r
      @VladyslavHoncharov-c6r Год назад

      Thank you a lot!!! I lost like 3 hours to find this info. Your coment helped me.

    • @johnsolly
      @johnsolly Год назад

      I just added a new video to my channel about adding a "smart search" for your Django app. It's a lot more powerful than the Q search I mention in this comment.

  • @michaelkovnick
    @michaelkovnick 2 года назад +11

    At 10:30 in this video venues = Venue.objects.filter(name__contains=searched) provided a case-sensitive search. Looking at the docs, it appears I need to add an "i" before contains to make it case-insensitive, resulting in: venues = Venue.objects.filter(name__icontains=searched). Perhaps this was a change in Django 4?

    • @jothamprince8765
      @jothamprince8765 2 года назад +1

      thanks a lot bro, was stuck on that bug the whole day, could u perhaps send a link to that part of the documentation so that i can read and learn more

  • @NITISHBANSAL101
    @NITISHBANSAL101 7 дней назад

    your teaching skills are cool

  • @DookyButter
    @DookyButter 3 года назад +5

    @8:05 The reason brackets work here is because he is indexing the dictionary through bracket notation, i.e. request.POST['foo']. If you use the get method of a dictionary, THEN you use parentheses, i.e. request.POST.get('foo'). That's why people would see a difference. Both are acceptable.

  • @Kennerdoll
    @Kennerdoll 2 года назад

    I guess I was just tired last night, today I have seen all the lines I forget, and thanks bro five stars for you.

    • @Codemycom
      @Codemycom  2 года назад

      Glad you got it sorted!

  • @deki90to
    @deki90to 3 года назад +14

    If someone have problem like me with printing foreign object to the template, solution is (foreign_object__object_name__icontains=search)

  • @asce313
    @asce313 3 года назад +2

    Oh wow, thanks John, was waiting for this video for quite sometime 😅

    • @Codemycom
      @Codemycom  3 года назад

      Hope you enjoyed it!

  • @MagicByIzzy
    @MagicByIzzy 3 года назад +1

    Love your videos John! I was the 1000th view!

    • @Codemycom
      @Codemycom  3 года назад +1

      Awesome! Thanks for watching!

  • @lorimedicenter4439
    @lorimedicenter4439 2 года назад

    your video is very fantastic...... coup de chapeau .... absolument dingue.... thank you very much .🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏🙏

    • @Codemycom
      @Codemycom  2 года назад

      Glad you enjoyed it!

  • @sathyanarayanan99
    @sathyanarayanan99 3 года назад +25

    At 8:11, you use either request.POST['param'] or request.POST.get('param'). It's parentheses whenever we use a getter.

  • @cyq3255
    @cyq3255 3 года назад

    Thanks for this video I was't sure where to start to add a search function for my django project and with your video I can now just adapt it to suit my needs. Very helpfull thanks.

  • @amirrezaasmaeizadeh3435
    @amirrezaasmaeizadeh3435 11 месяцев назад +1

    Thanks a lot for the best django learning page 🔥

    • @Codemycom
      @Codemycom  11 месяцев назад

      Welcome! Glad you liked it!

  • @iossamsun
    @iossamsun 3 года назад +1

    Eagerly waiting for your videos sir...

    • @Codemycom
      @Codemycom  3 года назад +1

      Thanks for watching!

  • @Alfakhri99
    @Alfakhri99 3 года назад

    Thanks a lot, I'm coming from your playlist Create a simple Django blog to add search bar feature. This video helped me a lot to implement it. your style is very easy to follow and less coding
    Thanks again.

    • @Codemycom
      @Codemycom  3 года назад +1

      Glad to hear you're enjoying the videos!

  • @kitewaves
    @kitewaves 2 года назад +1

    Thanks for great tutorials John - For the "search" field, would you advise to "Clean" it first before using it in the view to avoid XSS?

  • @technoiris9868
    @technoiris9868 3 года назад

    Thank you so much...I had been searching for such a straight forward tutorial...I'm glad I found one..🤗thanks a bunch..

    • @Codemycom
      @Codemycom  3 года назад +1

      Glad you liked it!

  • @KAILASHCHANDRA-jq6wf
    @KAILASHCHANDRA-jq6wf 7 месяцев назад

    GOOD ONE MY FRIENDS

  • @alexanderzittel8629
    @alexanderzittel8629 3 года назад +6

    Thanks John for your very helpful videos! I like them very much!
    If someone has the same problem with the views.py as me: I had to change the line to: searched = request.POST.get('searched')
    The both solutions shown in the video didn´t work for me.

  • @didiernzimbi2996
    @didiernzimbi2996 3 года назад

    So many thanks... it's really amazing how easy you make it look like. Thank you :)

  • @meirlanbekturganov7874
    @meirlanbekturganov7874 2 года назад

    BEST TUTORIAL JESUS!!!!!

  • @imediastv
    @imediastv Год назад

    realy best course for django search

  • @kittyrah2800
    @kittyrah2800 Год назад

    Is there a way to prevent the 'confirm form resubmission' popup from the search bar form only?

  • @danialazarpanah8875
    @danialazarpanah8875 2 года назад

    But what should we do if we want the text we have searched stay in the searched bar after doing the search? In this sample the text disappears after clicking the search button

  • @baguette184
    @baguette184 Месяц назад

    so helpful tyyy!!!

  • @stromigaming7970
    @stromigaming7970 Год назад

    Is that also workable in images in model?

  • @bidlby
    @bidlby 2 года назад

    you are amazing for sure i'll buy the full package :)

  • @slsantos100
    @slsantos100 2 года назад

    Thank you! very ...very so much. you are saved me...rrrrss

  • @smartlearning4857
    @smartlearning4857 3 года назад

    Excellent Sir

  • @muvi9230
    @muvi9230 3 года назад +2

    Thanks for video! but I get "" that , how can i fix this?

    • @Codemycom
      @Codemycom  3 года назад

      rewatch the video and try to figure out what you did differently

  • @hamdyramzy4350
    @hamdyramzy4350 3 года назад

    u are the best ♥

  • @oe669
    @oe669 Год назад

    Thank you man!

  • @Gemmmvoterpalautot1234
    @Gemmmvoterpalautot1234 3 года назад

    Thanks, John. ♥️

    • @Codemycom
      @Codemycom  3 года назад +1

      Thanks for watching!

  • @aguspe532
    @aguspe532 3 года назад

    Great video!

  • @iihhtt
    @iihhtt 3 года назад

    Great tutorial

  • @lonjezano
    @lonjezano 3 года назад

    Hey, thanks for this straightforward video but I want to paginate the search results. Since we checking for searched in the if, the else get executed when you try to go to the next page.
    So my question is how can we paginate the search results without running into errors or triggering the else statement

    • @Codemycom
      @Codemycom  3 года назад +2

      Good question, I'll have to do a video on it

  • @aykanseper
    @aykanseper 2 года назад

    Sir, thank you for the videos, you describe everything in perfect detail. But how can we search a name or something else inside of all the tables instead of just one table, in this case Venue. How do I query the entire database, for instance is there a way to change, venues = Venue.objects.filter(name__contains=searched) statement so that users would search the word in the entire database? Thanks again!

    • @Codemycom
      @Codemycom  2 года назад

      Sure, pretty sure I have videos on that sort of thing

  • @aminahrasool3564
    @aminahrasool3564 3 года назад

    Thank you, Sir! Would you please help to query all the records from a specific start date to an end based on the DateTime field? I'm facing an issue. Kindly guide me through!

  • @angelozanonaneto2228
    @angelozanonaneto2228 3 года назад

    Awesome!!

  • @timw3447
    @timw3447 2 года назад +2

    venues = Venue.objects.filter(name__contains=searched) -- was only showing case sensitive results for me
    after some searching: venues = Venue.objects.filter(name__icontains=searched) fixed that (note name__icontains was the change)

  • @brittvink8569
    @brittvink8569 2 года назад

    I keep getting an error if i use filter(id__contains=searched). Any idea how I can solve this? This is the error (1287, "1287: 'BINARY expr' is deprecated and will be removed in a future release. Please use CAST instead", None). I'm working with a mysql database

    • @Coffeenoosh-Viral
      @Coffeenoosh-Viral 2 года назад

      Hi, have you found any solution to this issue by any chance, I have the same issue...

  • @hiwab41
    @hiwab41 3 года назад

    Nice video!

  • @lashachan
    @lashachan 3 года назад

    Great tutorial as always. Can you do a multiple-column search Tutorial? Such as Excel filter when you can search for several columns.

  • @sorinbala703
    @sorinbala703 2 года назад

    i get a error like this: Field 'id' expected a number but got 'my search' how can i fix this?

    • @Codemycom
      @Codemycom  2 года назад

      what did you do differently from the video?

  • @seforaabigail6704
    @seforaabigail6704 2 года назад

    Thank u!!💗

  • @learnnow9598
    @learnnow9598 3 года назад

    Next for flask💙🤩

    • @Codemycom
      @Codemycom  3 года назад

      Flask fridays are awesome :-)

  • @darshantank554
    @darshantank554 3 года назад

    How to create one search bar for multiple fields. Like you can search users as well as Venues from one searchbar ?

    • @Codemycom
      @Codemycom  3 года назад +1

      Same way, just add another line like this to the views.py function:
      second_search= Venue.objects.filter(name__contains=searched)
      change Venue to whatever other model you want to search...then pass that variable to the context dictionary

    • @darshantank554
      @darshantank554 3 года назад

      @@Codemycom Good idea but,
      Don't you think it sounds like a temporary solution !?

    • @Codemycom
      @Codemycom  3 года назад +1

      @@darshantank554 In no way whatsoever is that a temporary solution.

  • @chrz4322
    @chrz4322 2 года назад

    Did anyone else have trouble getting the two html pages linked??? I've followed this tutorial exactly and when i click the search bar button it doesn't do anything! Any suggestions

    • @Codemycom
      @Codemycom  2 года назад

      Then you didn't follow the tutorial exactly. Recheck your code :-)

  • @codeKeshav
    @codeKeshav 3 года назад

    Sir John, what is your perception about *WhiteHatJr*

    • @Codemycom
      @Codemycom  3 года назад

      I don't know what that is

    • @codeKeshav
      @codeKeshav 3 года назад

      @@Codemycom
      Ok Leave that...
      API connected Currency Convertor
      # Connected with an API that provides latest currencies value.
      def getcurrentlist():
      import os
      os.system('pip install openexchangerate')
      from openexchangerate import OpenExchangeRates
      client=OpenExchangeRates(api_key='522e7989e9f64eb1ba21a035674dc275')
      global A,j
      k=client.currencies()[0]
      j=client.latest()[0]
      try:
      k.pop('VEF') # VEF has got Old
      except:
      pass
      A=[]
      for i in k:
      A.append([k[i],j[i]])
      A.sort()
      getcurrentlist()
      from tkinter import *
      from tkinter.ttk import Combobox
      root=Tk()
      root.title('Currency Convertor')
      ComboboxList_Name=[]
      for i in A:
      ComboboxList_Name.append(i[0])
      def load(e):
      if currency_Entry.get()=='':
      currency_Entry.insert(0,'0')
      x_name=Currency_combobox_1.get()
      for i in A:
      if i[0]==x_name:
      x_value=float(i[1])
      break
      y_name=Currency_combobox_2.get()
      for i in A:
      if i[0]==y_name:
      y_value=float(i[1])
      break
      feeded_value=float(currency_Entry.get())
      calc_value=f'{round((y_value*feeded_value)/(x_value),2)}'
      display.config(text=f'{feeded_value} {x_name}
      equals
      {calc_value} {y_name}')
      Currency_combobox_1=Combobox(root, font=('Agency FB','30','bold'), value=ComboboxList_Name)
      Currency_combobox_1.pack()
      Currency_combobox_1.set('Indian Rupee')
      Currency_combobox_1.bind('', load)
      currency_Entry=Entry(root, font=('Agency FB','30','bold'), borderwidth=0, bg='lightblue')
      currency_Entry.pack()
      currency_Entry.insert(0,round(j['INR'],2))
      currency_Entry.bind('', load)
      To_label=Label(root, text='To', font=('Agency FB','30','bold'))
      To_label.pack()
      Currency_combobox_2=Combobox(root, font=('Agency FB','30','bold'), value=ComboboxList_Name)
      Currency_combobox_2.pack()
      Currency_combobox_2.set('United States Dollar')
      Currency_combobox_2.bind('', load)
      display=Label(root, text=f'Rs. {round(j["INR"],2)} Indian Rupee
      equals
      ${j["USD"]} United States Dollar.', font=('Agency FB','30','bold'))
      display.pack(pady=30)
      root.mainloop()

  • @saivarshan8288
    @saivarshan8288 3 года назад

    If we search vegan(wrong spelling) instead of vegas. Will it return results? If not how to do it.

    • @Codemycom
      @Codemycom  3 года назад

      try it and see...why would it return results for a thing that doesn't exist? If I search pancakes, it won't return anything either :-p But, we wouldn't want it to, right?

    • @saivarshan8288
      @saivarshan8288 3 года назад

      @@Codemycom I am asking this commonly and not in this proj specifically. I am asking If the user mistypes will it return results related to the search

    • @saivarshan8288
      @saivarshan8288 3 года назад

      @@Codemycom I don't think it will. But how to do it.
      I created a proj where user can asks question and others can answer (like stackoverflow) and others can answer for it. If another user searches searches with related keywords in the question how to return results with related words searched by the user
      I know this is out of the scope of the project. But please help me out

    • @thetj7093
      @thetj7093 3 года назад

      @@saivarshan8288 really late and you've prbbly got it sorted out already but in case you have't, an immediate solution I can think of is training a searcher with some datasets so it can recognize mistyped words - such as vegan/vegaa/vegad/vegss for vegas, but this can get really complex if you've never used ML (an immediate DS I can think of is a decision tree) before.
      Then there's the dummy solution of including a list of mistyped versions for each keyword that the user can search by.
      Given how seemingly common this is, there should be exisiting algos/models that perform similarly, so you can give that a try too

  • @aravinths641
    @aravinths641 8 месяцев назад

    I have a case sensitive for characters problem what to do !!!! 😭

    • @Codemycom
      @Codemycom  8 месяцев назад

      Sorry I don't know what that is...

    • @aaronlal2676
      @aaronlal2676 7 месяцев назад

      try "icontains" instead of "contains"

  • @fasttracktechnologies
    @fasttracktechnologies 2 года назад

    Thank you so much

  • @ginalenetti5040
    @ginalenetti5040 3 года назад

    Hi, is this code available to look at?

    • @Codemycom
      @Codemycom  3 года назад

      Of course, it's always linked in the pinned comment.

  • @tamiltechguyz7264
    @tamiltechguyz7264 3 года назад

    Sir I am following all your videos but request to share ne the roles and permission access to templates in decoratorsq

    • @Codemycom
      @Codemycom  3 года назад

      No idea what you're talking about. There are no such things to share

  • @dvgpraveen
    @dvgpraveen Год назад

    What data inside the base. Html???

    • @Codemycom
      @Codemycom  Год назад

      just what the video shows...? What's the confusion?

  • @pj-pandaz2385
    @pj-pandaz2385 3 года назад

    def searchlist(request):
    if request.method == "POST":
    search_list= request.POST['search_list']
    searchfound = DogRescue.objects.filter(name__icontains= search_list)
    return render(request,'search.html',{"search_list": search_list,
    "searchfound": searchfound })
    else:
    return render(request,'search.html',{})
    Cannot resolve keyword 'name' into field. Choices are: Behavior, Characteristics, Hero, Hero_id, Location, Message, id .H ow to do please help? Thankyou

    • @xterminal
      @xterminal 3 года назад +1

      The error message you are receiving indicates the DogRescue table does not contain a name field. Substitute the column name where the search should be looking for "name" (e.g., if you want to search on values in the Behavior field, your filter should be "searchfound = DogRescue.objects.filter(Behavior__icontains =search_list)".

  • @sheddtutt7875
    @sheddtutt7875 3 года назад

    In your video description u made a typo error.
    It should be Venue.objects.filter(name__contains=searched)
    Not name__containts as you wrote

  • @anushav6304
    @anushav6304 3 года назад

    Can you please do a video of searching image

  • @chintanbrinda
    @chintanbrinda 3 года назад

    How can I search by address as well?

    • @Codemycom
      @Codemycom  3 года назад +1

      just filter by address instead of name in the query.

  • @F3ND1MUS
    @F3ND1MUS 3 года назад

    Ty

  • @snehlatasharma2411
    @snehlatasharma2411 3 года назад

    i got case error while searching.

    • @Codemycom
      @Codemycom  3 года назад

      how is that possible? :-p

  • @ahgpy
    @ahgpy 3 года назад

    the next playlist about this is discord.py bot maker so can u do it pls

  • @partialcoder6386
    @partialcoder6386 2 года назад +1

    hello :)

  • @midomezo9231
    @midomezo9231 3 года назад

    views get the data i can print it in terminal but it doesn't display it in template, tried everything
    def SearchView(request):
    if request.method == 'POST':
    searched = request.POST.get('searched')
    print(searched)
    return render(request, 'search/search.html', {
    searched:'searched'
    })
    else:
    return render(request, 'search/search.html', {})
    you searched for {{ searched }}
    or
    {% if searched %}
    you searched for {{ searched }}
    {% else %}
    You didn't search
    {% endif %}

  • @ahmadshareef6893
    @ahmadshareef6893 2 года назад

    where is the source code ):

    • @Codemycom
      @Codemycom  2 года назад

      In the pinned comment...just where I say it is in the video :-p

  • @abdullahatif7854
    @abdullahatif7854 3 года назад

    What about typo's and stuff. Sqlite3 can't deal with typo's and stuff. I was expecting something else. DISAPPOINTED !

    • @Codemycom
      @Codemycom  3 года назад

      lol what in the world are you talking about?

  • @rajasaraf6602
    @rajasaraf6602 3 года назад

    breaking bad

  • @eduardsanchez6962
    @eduardsanchez6962 3 года назад

    when i add the {%url 'show_venue' venue.id%} it gives me errors no Reverse for 'show_venue' with arguments '('',)' not found. 1 pattern(s) tried: ['show_venue/(?P[^/]+)$']