设置POST HTTP请求以创建变量.vnc fi

2024-06-02 13:48:29 发布

您现在位置:Python中文网/ 问答频道 /正文

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

在我的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 %}

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


Tags: 文件tonameipifrequesthtmltemplate
1条回答
网友
1楼 · 发布于 2024-06-02 13:48:29

这会给你一个想法:

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>

相关问题 更多 >