Search code examples
djangopython-3.xherokudjango-viewsheroku-postgres

405 error after implementing new search view


Currently using Django 2.1,Python 3.6, PostgreSQL 11, and hosting the DB on Heroku.

I am creating a web application that acts as a GUI database entry. I recently was provided with some search code here Django Front End Search. This code worked with a test application that has the server hosted on my machine. I am now seeing a 405 error when I try to communicate with my Heroku hosted database.

Currently my web app lists donors on donor_list.html. I would like for my user's query results to be posted on the donor_list.html after they perform their query.

Here is the error:

Method Not Allowed (POST): /device/donor_list/

Method Not Allowed: /device/donor_list/

"POST /device/donor_list/ HTTP/1.1" 405 0

# Associated urls
    path('donor_list/',views.DonorList.as_view(),name='donor_list'),
    path('donor_list/',views.SearchDonor,name='donor_search'),

# Donor Model
    class Donor(models.Model):
    name=models.CharField(max_length=265,blank=False)
    email=models.EmailField(max_length=265,blank=False)
    donation_house=models.ForeignKey(DonationHouse,
                                     default='1',
                                     related_name='dono_house',
                                     on_delete=models.CASCADE)

    def __str__(self):
        return self.name

# Donor View
    class DonorList(ListView):
    context_object_name = 'donors'
    model=models.Donor

# Search Code
    def SearchDonor(request):

    keywords=''

    if request.method=='POST': # form was submitted

        keywords = request.POST.get("ds", "")
        all_queries = None
        search_fields = ('name','email','donation_house__title')
        for keyword in keywords.split(' '):
            keyword_query = None
            for field in search_fields:
                each_query = Q(**{field + '__icontains': keyword})
                if not keyword_query:
                    keyword_query = each_query
                else:
                    keyword_query = keyword_query | each_query
                    if not all_queries:
                        all_queries = keyword_query
                    else:
                        all_queries = all_queries & keyword_query

        donorsearches = Donor.objects.filter(all_queries).distinct()
        context = {'donorsearches':donorsearches}
        return render(request, 'device_app/donor_list.html', context)

    else: # no data submitted

        context = {}
        return render(request, 'device_app/index.html', context)

# donor_list.html

    {% extends 'device_app/base.html' %}
    {% block body_block %}
    <div class="jumbotron">
    <h1>Donor List</h1>
    <p><a class="button" href="{% url 'device_app:donor_create'%}">
    Create  Donor</a>
    </p>
    <form method="POST" action=".">
          {% csrf_token %}
    <input id="search_box" type="text" name="ds"  placeholder="Search..." >
    <button class="button" type="submit" >Submit</button>
    </form>
    <br>
      {{donorsearches}}
    <br>
    <ul>
    {% for donor in donors %}
    <h6>
      <li>
        <a class="annoying" href="{{donor.id}}">{{donor.name}}</a>
      </li>
    </h6>
    {% endfor %}
    </ul>
    </div>
    {% endblock %}

Solution

  • If you want the search results to show on the same page, you need to have them in the same view, not a separate one.

    class DonorList(ListView):
        context_object_name = 'donors'
        model=models.Donor
    
        def post(self, request):
            keywords = request.POST.get("ds", "")
            all_queries = None
            search_fields = ('name','email','donation_house__title')
            for keyword in keywords.split(' '):
                ...
    

    Although note, for searches it's usual to use a GET request, not a POST.