EilonA
EilonA

Reputation: 581

Simple search engine in my Django Web is not working

I'm trying to make my search go over my query my Database in Django.

The lines in form.is_valid() is other forms I added to my website of adding data and it works perfect. The only thing is that query thing.. I just don't understand why after I click search I still see the all posts and not the posts I search for..

views.py-

class HomeView(TemplateView):

    template_name = 'serverlist.html'

    def get(self, request):
        form = HomeForm()
        query = request.GET.get("q")
        posts = serverlist.objects.all()
        args = {'form' : form, 'posts' : posts}
        return render(request, self.template_name, args)

    def post(self,request):
        form = HomeForm(request.POST)
        posts = serverlist.objects.all()

        query = request.GET.get("q")
        if query:
            posts = serverlist.filter(Project__icontains=query)

        if form.is_valid(): # Checks if validation passed
            post = form.save(commit=False)
            post.save()
            text = form.cleaned_data['ServerName']
            form = HomeForm()
            return redirect('serverlist')
        args = {'form': form, 'text': text}
        return render(request, self.template_name,args)

index.html -

<div class="container">

    <br>
    <center><h1>DevOps Server List</h1></center>
    <br>
    <form method='GET' action=''>
        <input type='text' name='q' placeholder='Search Item'/>
        <input type='submit' value='Search' />
    </form>
    <table class="table table-hover">
    <thread>

      <tr>
        <th> Server Name </th>
        <th> Owner </th>
    <th> Project </th>
    <th> Description </th>
    <th> IP Address </th>
    <th> ILO </th>
    <th> Rack </th>
    <th> Status </th>
    <th>  </th>


</tr>
</thread>
<tbody>

            {% for server in posts %}

    <tr>
      <div class ="server">
        <td>{{ server.ServerName }}</td>
        <td>{{ server.Owner }}</td>
        <td>{{ server.Project }}</td>
        <td>{{ server.Description }}</td>
        <td>{{ server.IP }}</td>
        <td>{{ server.ILO }}</td>
        <td>{{ server.Rack }}</td>
        <td>{{ server.Status }}</td>

        <td>

forms.py -

class HomeForm(forms.ModelForm):
    ServerName = forms.CharField(widget=forms.TextInput,max_length = 30)
    Owner = forms.CharField(max_length = 50)
    Project = forms.CharField(max_length = 30)
    Description = forms.CharField(max_length = 255)
    IP = forms.CharField(max_length = 30)
    ILO = forms.CharField(max_length = 30)
    Rack = forms.CharField(max_length = 30)
    Status = forms.CharField(max_length = 30)
    class Meta:
        model = serverlist
        fields = ('ServerName' ,'Owner','Project','Description','IP','ILO','Rack','Status',)

Upvotes: 0

Views: 631

Answers (2)

user8060120
user8060120

Reputation:

you should add filter to your get methods:

posts = serverlist.objects.all() 
query = request.GET.get("q")
if query:
    posts = serverlist.objects.filter(Project__icontains=query)
    #                          ^^^^^^^^^^^^^^^^^^^^^^^

Upvotes: 1

scriptmonster
scriptmonster

Reputation: 2771

you are performing the search in the post method. Thus you need to send data with POST method. Please change the method in the form tag as follows.

<form method='POST' action=''>
    {% csrf_token %}
    <input type='text' name='q' placeholder='Search Item'/>
    <input type='submit' value='Search' />
</form>

When you use POST method you will need csrf tooken as well. {% csrf_token %} template tag is used for that purpose.

BTW you are using a form object to validate your form. But you created the form manually in your template. You should use it to create HTML too. Thus your template will be like this:

<form method='POST' action=''>
    {% csrf_token %}
    {{ form }}
    <input type='submit' value='Search' />
</form>

This could solve your problem about form not being valid. Since you did not share your HomeForm sources, I cannot tell.

Upvotes: 0

Related Questions