我想使用JavaScript语法

时间:2018-06-13 14:35:31

标签: javascript python django django-models django-forms

所以,我希望从我的表单(addDatabase.html)获取用户输入并显示他们输入的内容的输出,并让他们下载带有yaml格式输出(output.html)的txt文件。

当我使用request.POST [“fieldname”]使用Django的models.py时,它正在工作。 我所做的是使用JS来获取innerHTML的标签并显示出它的放置

output.html JS代码

        var name = document.getElementById("name");
        formContents += name.innerHTML + "\n";


        var type = document.getElementById("type");
        formContents += "  " + type.innerHTML + "\n";

但我现在的问题是因为我决定使用formform而不是model.py来使用form.py。我不知道如何从我的模型中检索数据,就像我之前从模型中检索到的那样。

output.html

<li id="type">type: {{ form.type}}</li>
<li id="vlanid">vlan_id: {{ form.vlan_id}}</li>
<li id="rdp">rdp: {{ form.rdp}}</li>
<li id="subnet">subnet:</li>

View.py(使用models.py中的模型)

def output(request):
    Form_info.objects.all()

    print("The button is clicekd")
    name = request.POST["name"]
    type = request.POST["type"]
    vlan_id = request.POST["vlanid"]
    rdp = request.POST["rdp"]

    ip_master = request.POST["ipmaster"]
    ip_backup = request.POST["ipbackup"]
    vip = request.POST["gateway"]
    nat_ip = request.POST["natip"]

    cidr = request.POST["cidr"]
    dns = request.POST["dns"]

    dhcp = request.POST["dhcp"]
    dhcp_pool_start = request.POST["dhcpstart"]
    dhcp_pool_end = request.POST["dhcpend"]

    form_info = Form_info(name=name, type=type, vlan_id=vlan_id, rdp=rdp, ip_master=ip_master, ip_backup=ip_backup,vip=vip, nat_ip=nat_ip, cidr=cidr, dns=dns, dhcp=dhcp, dhcp_pool_start=dhcp_pool_start,dhcp_pool_end=dhcp_pool_end)

    form_info.save()

    context = {
       'form': form_info
    }

    return render(request, 'niro/output.html')

**** View.py使用forms.py ****

中的modelform

这是我以前的addDatabase函数

def addDatabase(request):
    # if this is a POST request we need to process the form data
    return render(request, 'niro/addDatabase.html')

这是我当前的AddDatabase函数

def addDatabase(request):
    if request.method == 'POST':
        form = YamlForm(request.POST)
        if form.is_valid():
            form.save()

        # return HttpResponseRedirect('niro/output.html', {'form': form})
        return HttpResponseRedirect('.', {'form': form})
    else:
        form = YamlForm()

    return render(request, 'niro/addDatabase.html', {'form': form})

addDatabase.html 因此,当点击按钮时,在此处发布重定向到output.html函数

  <div class="container">
 <form action="{% url "output" %}" method="post"> 
{% csrf_token %}

<div class="row name">
<h3>NETWORK</h3>
</div>
<div class="row">
  <div class="col-25">
    <label for="">NAME:</label>
  </div>
  <div class="col-75">
      <span class="text-danger" small>{{ form.name.errors }}</span>
        {{ form.name }}

  </div>

</div>
<div class="row">
  <div class="col-25">
    <label for="type">TYPE:</label>
  </div>
  <div class="col-75">
    <span class="text-danger" small>{{ form.type.errors }}</span>
      {{ form.type }}



.
.
.
.
    <div class="row button container">
    <div class="row button">
        <button type="submit" value="submit">SUBMIT</button>
        <button type="submit" value="submit">ADD MORE</button>

    </div>
</div>

Models.py

BOOLEAN_CHOICES = (
(None, ' '),
(True, 'true'),
(False, 'false')
)


class Form_info(models.Model):
 # all the columns

# example = models.CharField(null=Ture, unique=True)
name = models.CharField(max_length=50, null=True)
type = models.CharField(max_length=1, null=True)
# vlan_id = models.DecimalField(max_digits=4, decimal_places=0, null=True)
vlan_id = models.CharField(max_length=4, null=True)
rdp = models.CharField(max_length=10, null=True)

ip_master = models.CharField(max_length=15, null=True)
ip_backup = models.CharField(max_length=15, null=True)
# vip = models.GenericIPAddressField(protocol="ipv4")
vip = models.CharField(max_length=15, null=True)
nat_ip = models.CharField(max_length=15, blank=True)

# subnetmask might be devided into 2 different fields
cidr = models.CharField(max_length=18, null=True)
dns = models.CharField(max_length=15, null=True)

# boolean dropdown doesnt show
dhcp = models.CharField(null=True, max_length=10)
dhcp_pool_start = models.CharField(max_length=15, null=True)
dhcp_pool_end = models.CharField(max_length=15, null=True)


def get_absolute_url(self):
    return reverse('niro:output', kwargs={'pk': self})

def __str__(self):
    print("This is models.py")
    return self.name + self.name + self.vlan_id + self.rdp + self.cidr + self.dhcp

所以我想在两个函数上修改我的views.py,这样我就可以从表单中显示userinput,并使用Django modelform启用现有JS代码的下载功能。请帮帮我!!

0 个答案:

没有答案