Note that there are some explanatory texts on larger screens.

plurals
  1. POReturn Django Models for Template Rendering after an Ajax Request
    text
    copied!<p>I would like to create an AJAX-based search for my webpage. So far I am able to send the form data and make the appropriate call to my Django model. What I am having a hard time with is just sending the Queryset back and having it rendered using the Django templating system. Your help/advice is greatly appreciated.</p> <p>Here is the code I am working with.</p> <p>views.py</p> <pre><code>if request.is_ajax(): if request.method == 'POST': format = 'json' mimetype = 'application/json' try: q = request.POST['obj'] o = Object.objects.filter(name__icontains=q) return render_to_response( 'project_view_objects.html', {'username': request.user.username, 'results':o}) </code></pre> <p>view.html</p> <pre><code>&lt;script&gt; $(document).ready(function(){ $("#search_form").submit(function(event) { event.preventDefault(); $.ajax({ type: "POST", url: "/objects/search/", data: $(this).serialize(), processData: false, dataType: "json" }); });}); &lt;/script&gt; &lt;article&gt; &lt;blockquote&gt; &lt;form class="create_form" id="search_form"&gt; &lt;p&gt; &lt;input id="objectSearchNameInput" type="text" name="obj" value="Object name"&gt; &lt;input type="submit" value="search objects"&gt; &lt;/p&gt; &lt;/form&gt; &lt;/blockquote&gt; &lt;/article&gt; &lt;br /&gt; {% if results %} &lt;blockquote&gt; &lt;aside class="column"&gt; {% for object in results %} &lt;b&gt;&lt;a href="#" class="extra-text-special"&gt;{{ object.name }}&lt;/a&gt;&lt;/b&gt;&lt;br /&gt; {% endfor %} &lt;/aside&gt; &lt;aside class="column"&gt; {% for object in results %} &lt;font class="extra-text-nospecial"&gt;{{ object.created_when }}&lt;/font&gt;&lt;br /&gt; {% endfor %} &lt;/aside&gt; &lt;/blockquote&gt; {% else %} &lt;p&gt;haha&lt;/p&gt; {% endif %} </code></pre> <p>At the moment, all I see displayed on the page is 'haha'.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload