Django表单返回元组而不是字符串

时间:2019-02-20 15:15:04

标签: python django

我已经建立了一个网页来展示我的团队自动化脚本,该页面应该返回一个从下拉菜单压缩成字符串的字典。

我得到的是一个元组,而不是字符串。我需要返回一个正常的字符串,但不知道该元组的原因。

观看次数:

import datetime
import json
from django.shortcuts import render, redirect
from django.http import HttpResponse
from django.contrib.auth import authenticate
from django.template import loader
from django.urls import reverse
from django.contrib import auth
from home.models import ChangeLog
from health.models import FirewallInfo
from . import forms
from . import sftpadd

def ipadd_index(request):
    if request.user.is_staff:
        ipadd = forms.IPAdd()
        return render(request, 'ipadd/index.html', {'ipadd': ipadd})
    else:
        return redirect('/unauthorized/')

def ipadd_action(request):
    if request.method == 'POST':
        form = forms.IPAdd(request.POST)
        if form.is_valid():
            casenum = request.POST.get('casenum')
            ipaddr = request.POST.get('ipaddr')
            objgp = request.POST.get('objgp')
            cidr = request.POST.get('cidr')
            objgp = ''.join(objgp)
            objgp = json.loads(objgp)
            asa = []
            for k, v in objgp:
                OBJGP = k
                for item in v:
                    firewall = FirewallInfo.get(fw_name=item)
                    asa.append(firewall.fw_ip)
            for fw in asa:
                if cidr == 'HOST':
                    sftpadd.sftpadd_host(ipaddr, fw)
                else:
                    sftpadd.sftpadd_sub(ipaddr, cidr, fw)
                sftpadd.save(fw)
            dbdetails = str(ipaddr) + ' has been added to ' + str(OBJGP)
            dbentry = ChangeLog(
                user_name=request.user.username,
                change_date=datetime.datetime.today().strftime('%Y-%m-%d'),
                change_cr='N/A',
                change_type='IP Add',
                details=dbdetails,
                case_num=casenum)
            dbentry.save()
            item = ChangeLog.get(pk=dbentry.pk)
            return render(request, 'change/detail.html', {'item': item})
        else:
            return redirect('/Oops/')
    else:
        return redirect('/ipadd/')

表格:

from django import forms
from . import models

class IPAdd(forms.Form):
    CIDR = [
        ('HOST', 'Host'),
        (24, '/24'),
        (25, '/25'),
        (26, '/26'),
        (27, '/27'),
        (28, '/28'),
        (29, '/29'),
        (30, '/30'),
        (31, '/31'),
    ]
    OBJGP = [
        (
            None,
            'Select',
        ),
        (
            {"EXT-SFTPALLOWED": ["IPAYDEVDMZSMO-FW", "IPAYATX-EDGE-FW", "IPAYPRODDMZBMO-FW", "IPAYDEVDMZSMO-FW"]},
            'EXT-SFTPALLOWED',
        ),
    ]
    casenum = forms.IntegerField(
        widget=forms.NumberInput(attrs={'class': 'form-control', 'placeholder': 'Case#'}),
        required=True,
        label="")
    ipaddr = forms.GenericIPAddressField(
        widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'IP Address'}),
        required=True,
        label='')
    cidr = forms.ChoiceField(
        widget=forms.Select(attrs={'class': 'form-control', 'placeholder': 'Type'}),
        choices=(CIDR),
        label='')
    objgp = forms.ChoiceField(
        widget=forms.Select(attrs={'class': 'form-control', 'placeholder': 'Type'}),
        choices=(OBJGP),
        label='')

html值:

<option value="{&#39;EXT-SFTPALLOWED&#39;: [&#39;IPAYDEVDMZSMO-FW&#39;, &#39;IPAYATX-EDGE-FW&#39;, &#39;IPAYPRODDMZBMO-FW&#39;, &#39;IPAYDEVDMZSMO-FW&#39;]}">
    EXT-SFTPALLOWED
</option>

POST在“ objgp”中显示元组:

casenum     '1234'
objgp       ("{'EXT-SFTPALLOWED': ['IPAYDEVDMZSMO-FW', 'IPAYATX-EDGE-FW', 'IPAYPRODDMZBMO-FW', 'IPAYDEVDMZSMO-FW']}")
csrfmiddlewaretoken     '______________________________'
ipaddr      '10.1.1.10'
cidr        'HOST'

0 个答案:

没有答案