2013-08-25 62 views
0

我有一個包含用戶信息和IP的數據庫。我想要做的是動態創建,然後用他們的IP打開一個* .vnc文件。設置POST HTTP請求創建變量.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文件。

回答

0

這應該給你一個想法:

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

感謝您的答覆!我能夠使用您發送的內容並使其工作。我只是添加了你發送給我的視圖並使按鈕下載了該文件。我將探索如何下載並立即打開文件。再次感謝! – user2716271