Home > Enterprise >  How do I create a Django / Ajax redirect POST request on click?
How do I create a Django / Ajax redirect POST request on click?

Time:04-08

I have a search function in my django app using ajax. It displays all results from a model.

I want to limit the number of results the search returns on the page though, so when the queryset is so large the user should click on a 'see all results', rather than having heaps of results in the search box.

I'm trying to do this by adding a HTML form below, but when I click on the href it is creating a GET request. I'm not totally sure what I need to fix up here to pass the data to the view and render the new page.

resultsBox.innerHTML  = `
    <a href="${url} ${'results'}" >
        <form method="POST"  input type="submit"
            {% csrf_token %}
            <action="${url} ${'results'}">
        </form>
        <div >
            <div >
                img placeholder
            </div>
            <div >
                <h5>See all results for "${quote}"</h5>
            </div>
        </div>
    </a>
`           `

CodePudding user response:

You are wrapping the entire form in the href, you probably just want See all results to be the link?

resultsBox.innerHTML  = `
    
        <form method="POST"  input type="submit"
            {% csrf_token %}
            <action="${url} ${'results'}">
        </form>
        <div >
            <div >
                img placeholder
            </div>
            <div >
                 <h5>
                   <a href="${url} ${'results'}" >
                      See all results for "${quote}"
                   </a>
                 </h5>
            </div>
        </div>`
  • Related