0

I am trying to render only the table for the response to the AJX call, but it renders the whole template. How can I only render the table while using render_to_response?

Template:

<html>
    <head>
        <title> Hash Searcher </title>
        <script type="text/javascript" src="{{STATIC_URL}}jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                $('#searchSubmit').click(function() {
                    q = $('#q').val();
                    $('#results').html('&nbsp;').load('/?mdhash=' + q );

                });
            });
            $(document).ajaxStart(function() {
                $('#spinner').show();
            }).ajaxStop(function() {
                $('#spinner').hide();
            });
        </script>
    </head>
    <body style="text-align: center; ">
        <input id="q" type="text" name="mdhash" value="{{ mdhash }}" style="text-align: center; "size=60 />
        <br>
        <input id="searchSubmit" type="submit" value="Search" />
        <br>
        <div style="display:none" id="spinner"><img src="{{STATIC_URL}}spinner.gif"/></div>
        <br>
        <span id="results">
        {% if plain %}
        <table>
            <tbody>
                <tr>
                    <td><small>#</small></td>
                    <td><small>Hash</small><br></td>
                    <td><small>Clear</small><br></td>
                    <td><small>Type</small><br></td>
                    <td><small>DB</small><br></td>
                </tr>
                {% for hash, clear in plain.items %}
                <tr>
                    <td><small>{{ forloop.counter }}</small></td>
                    <td><small>{{ hash }}</small><br></td>
                    <td><small><strong>{{ clear.0 }}</strong></small><br></td>
                    <td><small>{{ clear.1 }}</small><br></td>
                    <td><small>{{ clear.2 }}</small><br></td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
        {% endif %}
        </span>
    </body>
</html>

View:

from django.shortcuts import render_to_response
from django.http import HttpResponse
from django.template.context import RequestContext
import core.models, time

def search(request):
    req = request.GET.get('mdhash', '')
    plain = {}
    try:
        plain[req] = (core.models.Hash_md5.objects.get(hash=req).plain, 'md5', 'iCrack')
    except:
        pass
    #aif request.is_ajax():
    #    time.sleep(5)
    #    return HttpResponse({plain: 'plain'})
    #else:
    return render_to_response('index.html', locals(), context_instance = RequestContext(request))

1 Answer 1

1

If you factor the <table> part of the template out (and include it in the page template using {% include ... %}), then you can just render that sub-template if request.is_ajax() and you'll be able to replace just that part of the page via jQuery.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.