简体   繁体   中英

Trying to render a python dictionary into django template with javascript

Im trying to render a python dictionary into my django template using ajax. I have some results from the whoisLookup which i want to render in certain table rows. Be gentle im litteraly new to django and JavaScript!

My views.py code

def whois_lookup(request):
    if request.is_ajax():
        domain = request.session.get('domain_or_ip')
        res = whois.whois(domain)
        data = {'message': 'whois-lookup completed.',
                'domain_name': res.domain_name
                }
        return HttpResponse(json.dumps(data), content_type='application/json')
    else:
        raise Http404

what i tryed in my.js file.

$(document).ready(function () {
$('#whois-btn').click(function () {
    $.ajax({
        type: "GET",
        url: "/WanDashboard/whoisLookup/",
        beforeSend: () => {
            $(".ajax_loader").show();
            console.log('BeforeSend whoisLookup');
        },
        success: function (data) {
            # this works.
            alert(data.message);
            # this does not work
            alert(data.domain_name)
        },
        complete: () => {
            $(".ajax_loader").hide();
            console.log('Completed whois ajax request.');
        }
    });
});

});

the.html page

<div id="whois-results">
<table class="table table-dark" id="result_whois_table">
    <thead>
    <tr>
        <th scope="col">Domain Name</th>
        <th scope="col">Whois Server</th>
        <th scope="col">Name Servers</th>
        <th scope="col">Contact mails</th>
        <th scope="col">Address</th>
        <th scope="col">City</th>
    </tr>
    </thead>
    <tbody>
    <tr>
        <td>{{ data.domain_name }}</td>
    </tr>
    </tbody>
</table>
domain = request.session.get('domain_or_ip')

Im not sure if the code you have added here is the exact same as in your application - but the.get() needs a valid parameter. eg

domain = request.session.get('https://api.github.com/user')

Your whois_lookup function will return a JSON dump to the url that you have assigned to it in your urls.py file.

So your templates will not work as they are not setup as @vishal has mentioned in the comments.

Kinda late but i managed to do it like this.

views.py code

def whois_lookup(request):
if request.is_ajax():
    domain = request.session.get('domain_or_ip')
    res = whois.whois(domain)
    l_nameservers = res.name_servers
        nameservers = ' '.join([str(elem) for elem in l_nameservers])
    whois_data = {
        'message': 'Completed-scan!',
        'nameservers': nameservers
    }
    return JsonResponse(whois_data, safe=False)
else:
    raise Http404

and the JavaScript code that helped me do the job.

$(document).ready(function () {
$('#whois-btn').click(function () {
    $.ajax({
        type: "GET",
        url: "/WanDashboard/whoisLookup/",
        dataType: 'json',
        beforeSend: () => {
            $(".ajax_loader").show();
            console.log('BeforeSend whoisLookup');
        },
        success: function (whois_data) {
            let splitted_array_nameservers = whois_data.nameservers.split("");
            $("#result_whois_table").show();
            let whois_table = $("#result_whois_table tbody");
            whois_table.append("<tr><td>" + whois_data.nameservers + "</td></tr>")
        },
        complete: () => {
            $(".ajax_loader").hide();
            console.log('Completed ajax.');
        }
    });
});

});

This is a really sketchy sollution though!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM