【问题标题】:Setup POST HTTP Request to create variable .vnc file设置 POST HTTP 请求以创建变量 .vnc 文件
【发布时间】:2013-08-28 09:15:37
【问题描述】:

我有一个包含用户信息和 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 文件。

【问题讨论】:

    标签: python html django post vnc


    【解决方案1】:

    这应该会给你一个想法:

    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
    

    模板

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

    【讨论】:

    • 感谢您的回复!我能够使用您发送的内容并使其正常工作。我刚刚将您发送给我的内容添加到我的视图中,并让按钮下载文件。我将探索如何让它下载,然后现在打开文件。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-10
    • 1970-01-01
    • 1970-01-01
    • 2019-01-10
    • 1970-01-01
    • 1970-01-01
    • 2018-10-12
    相关资源
    最近更新 更多