如何保留文本框并提交按钮并在同一页面中显示输出

时间:2017-06-22 17:34:23

标签: django django-forms django-templates

views.py

def nslookups(request):
dig = NameForm
r = []
#dig = form_class(request.POST or None)
if request.method == 'POST':
   dig = NameForm(request.POST)
   if dig.is_valid():
       fqdn = dig.cleaned_data['lookup']
       answers = dns.resolver.query( fqdn,'A')
       for a in answers:
            r.append(a.address)
            print(r)
       return render(request,'accounts/dns.html',{'nslookup': r})

else:
    dig = NameForm()
return render(request, 'accounts/dns.html',{'dig': dig})

dns.html

{% extends 'base.html' %}

{% block content %}
<h3>Welcome</h3>
<h3>DNS</h3>

<form  method="post">
   {% csrf_token %}
   {{ dig }}
   <input type="submit" value="Submit">
</form>
{{ nslookup }}
{% endblock %}

要求是在文本框中输入dns记录,然后在文本框和提交按钮的同一页面中显示结果。

0 个答案:

没有答案