Django:如何在模板中填充表单数据

时间:2018-03-21 14:29:02

标签: python django python-3.x django-forms

所以我想尝试两种形式"在单个基于的视图中。

我的观点是:

class ListAndCreate(CreateView):
    model = xmpp_buddy_groups
    form_class = xmpp_buddy_groups_form
    second_form_class = sip_extension_form
    template_name = "xmpp/index.html"
    success_url = reverse_lazy('xmpp:index')

    def get_context_data(self, **kwargs):
        context = super(ListAndCreate, self).get_context_data(**kwargs)
        context['object_list'] = self.model.objects.all()
        extension = SipExtension.objects.values_list('sip_extension', flat=True)
        obj = SipExtension.objects.filter(sip_extension=1331).first()
        for buddy_groups in group_names:
            for sip in buddy_groups.sipextension_set.all():
                sip_extension = sip.sip_extension
                print(sip_extension)
        context['extension'] = extension
        SipExtension.objects.exclude(sip_extension__in=extension)
        print(extension)
        context['form'] = self.form_class
        context['form2'] = self.second_form_class(instance=obj)
        context['extensions'] = SipExtension.objects.exclude(sip_extension__in=extension)
        return context

我的表单类:

from django import forms
from .models import xmpp_buddy_groups
from extensions.models import SipExtension
class xmpp_buddy_groups_form(forms.ModelForm):

    class Meta:
      model = xmpp_buddy_groups
      fields = ['group_name']

    def __init__(self, *args, **kwargs):
     super(xmpp_buddy_groups_form, self).__init__(*args, **kwargs)
     self.fields['group_name'].required = False

class sip_extension_form(forms.ModelForm):
   class Meta:
     model = SipExtension
     fields = ['real_name','sip_extension']
     widgets = {
            "sip_extension": forms.Select
           }
   def __init__(self, *args, **kwargs):
     super(sip_extension_form, self).__init__(*args, **kwargs)
     self.fields['sip_extension'].queryset = \
        SipExtension.objects.filter(sip_extension=1331)

我的模特:

from django.db import models
from django.urls import reverse
class xmpp_buddy_groups(models.Model):
    group_name = models.CharField(max_length=30,unique=True)

    def __str__(self):
        return '%s' % (self.group_name)


from django.db import models

class SipExtension(models.Model):
    real_name = models.CharField(max_length=32)
    sip_extension = models.PositiveIntegerField(unique=True)
    sip_secret = models.CharField(max_length=32)
    commlink_push = models.BooleanField(default=False)
    default_did = models.ForeignKey('dids.DidNumber',null=True,blank=True)
    xmpp = models.BooleanField(default=False)
    xmpp_username = models.CharField(max_length=50,default='username')
    xmpp_password = models.CharField(max_length=32,default='password')
    xmpp_buddy_groups_names = models.ManyToManyField('xmpp.xmpp_buddy_groups')


    def __str__(self):
        return '%s : %s'  % (self.sip_extension, self.real_name)

我在模板中访问表单{{form2.as_p}}。下拉列表显示为空。我希望它填充1331。我怎么能这样做?

编辑:添加的模型可以让问题更加清晰。请原谅我对Django的经验不足

0 个答案:

没有答案