0

我有一个包含用户信息和 IP 的数据库。我想做的是动态创建,然后用他们的 IP 打开一个 *.vnc 文件。

在我的views.py文件中,我有这个:

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})

注释掉的部分正是我一直在玩的,也是我目前认为我需要做的。

我的模板文件list.html如下所示:

{% 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 %}

我想做的是单击“创建 VNC”按钮,然后创建并打开 *.vnc 文件。

4

1 回答 1

0

这应该给你一个想法:

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

视图.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

模板

<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>
于 2013-08-26T14:41:49.483 回答