Question

I have a database with users information and IP's. What I would like to do is to dynamically create, and then open, a *.vnc file with their IP.

In my views.py file I have this:

def view_list(request):
    template_name = 'reader/list.html'
    cust_list = xport.objects.all()
    #if request.method == 'POST':
        #<a href=link to created *.vnc file>Connect to client</a>
    return render(request, template_name, {'xport': cust_list})

The commented out portion is just what I've been playing with and what I currently think I need to do.

My template file is list.html and looks like this:

{% extends "base.html" %}
{% load url from future %}


{% block content %}
<h1> Customer List </h1>

<ul>
    {% for c in xport %}
        {% if c.ip %}
            <li>{{ c.firstName }} {{ c.lastName }}</li>
            <form method="POST" action=".">{% csrf_token %}
                <input type="submit" name="submit" value="Create VNC to {{ c.ip }}" />
            </form>
        {% endif %}
    {% endfor %}
</ul>
{% endblock %}

What I would like to do is to click on the "Create VNC" button and then that create and open the *.vnc file.

Was it helpful?

Solution

This should give you an idea:

url(r'^file/vnc/$', 'myapp.views.vnc', name='vnc-view'),

views.py

from django.views.decorators.http import require_POST

@require_POST
def vnc(request):
    ip = request.POST.get('ip', None)
    response = HttpResponse(ip, content_type='application/octet-stream')
    # If you don't want the file to be downloaded immediately, then remove next line
    response['Content-Disposition'] = 'attachment; filename="ip.vnc"'
    return response

template

<form method="POST" action="{% url 'vnc-view' %}">{% csrf_token %}
  <input type="hidden" name="ip" value="127.0.0.1" />
  <input type="submit" name="submit" value="Create VNC to 127.0.0.1" />
</form>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top