Home > Software design >  how can i put a search box in my django app?
how can i put a search box in my django app?

Time:07-31

my code trying to make a search box for searching books is here, im trying to make a search box, when user enters the name of a book , like google, it be shown on page , when i add {{form}} to book.html file it has to shows a box, but it doesnt ,

views.py:

def books(request):
      if request.method == 'POST':
        form = DashboardFom(request.POST)
        text = request.POST['text']
        url = 'https://www.googleapis.com/books/v1/volumes?q=' text
        r = requests.get(url)
        answer = r.json()
        result_list = []
        for i in range(10):
            result_dict = {
                'title':answer['items'][i]['volumeInfo']['title'],
                'subtitle':answer['items'][i]['volumeInfo'].get('subtitle'),
                'description':answer['items'][i]['volumeInfo'].get('description'),
                'count':answer['items'][i]['volumeInfo'].get('pageCount'),
                'catagories':answer['items'][i]['volumeInfo'].get('catagories'),
                'rating':answer['items'][i]['volumeInfo'].get('pageRating'),
                 'thumbnail':answer['items']   [i]['volumeInfo'].get('imageLinks').get('thumbnail'),
                 'preview':answer['items'][i]['volumeInfo'].get('previewLink')
            }
            result_list.append(result_dict)
            context={
                'form':form,
                'result':result_list,
            }
        return render(request,'dashboard/books.html',context)            
    else:
        form = DashboardFom()
    context = {'form':form}
    return render(request,'dashboard/books.html',context)

forms.py:

class DashboardFom(forms.Form):
    text = forms.CharField(max_length=100,label='Enter your search : ')

and also my books.html:

    {% extends 'dashboard/base.html' %} 
    {% load static %} 
    {% block content %}
    <section class='text-center container'>
        <h2>Search books and browse your favorite</h2>
        <p>just enter the search query to obtain the results</p><b></b>
        <form action="" method="POST">
            {% csrf_token %}
            {{form}}
            <input  type="submit" value="Submit">
        </form><br>

    {% for result in results %}
        <a href="{{result.preview}}" target="_blank">
            <div >
                <div >
                    <div >
                        <div >
                            <img  src="{{result.thumbnail}}" alt="">

                        </div>
                        <div >
                            <h3 >{{result.title}}</h3>
                            <b>
                                <u>
                                    <h5 >{{result.subtitle}}</h5>
                                </u>
                            </b>
                            {% if result.description %}
                                <h6 >{{result.description}}</h6>
                            {% endif %}
                            <b> 
                            {% if result.categories %}
                                <h6 >Category: 
                                    {% for category in result.categories  %}
                                        {{category}} 
                                    {% endfor %}
                                </h6>
                                {% endif %}
                                {% if result.count %}
                                <h6 >Pages: {{result.count}}</h6>
                                {% endif %}
                                {% if result.rating %}
                                <h6 >Rating: {{result.rating}}</h6>
                                {% endif %}
                            </b>
                        </div>
                    </div>
                </div>
            </div>
        </a>
    {% endfor %}
        <br>
    </section>
    {% endblock content %}

do you have any ideas that what it happens? also im making a youtube part just like that, icant see the box there too.

CodePudding user response:

text = forms.CharField(max_lenght=100, label='...', widget=forms.TextArea())

https://docs.djangoproject.com/en/4.0/ref/forms/widgets/

CodePudding user response:

Refer to this video. It explains how to add a search bar to your app. That person's app might be different but follow what he says, and when you've done it correctly and followed the steps, you'll have your search bar ready...

https://www.youtube.com/watch?v=AGtae4L5BbI

  • Related