▶️ Watch Entire Django Wednesdays Playlist ✅ Subscribe To My RU-vid 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
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?
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!
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.
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.
@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.
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)
Actually this is my first comment in RU-vid. 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.
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 %}
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
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
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)".
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
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
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!
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!
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[^/]+)$']
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.
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
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.
@@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()
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?
@@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
@@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
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