使用自定义表单模板时,Django在表单中选择有效的选择错误

时间:2017-08-01 09:20:21

标签: python django choicefield modelchoicefield

我现在已经潜伏了很长一段时间,但我找不到解决问题的方法。 我为我的表单制作了自定义模板,现在,当我尝试提交表单时,我会在选择字段下选择:选择一个有效的选项。不是可用的选择之一。

我认为问题是因为我没有通过组织实例而是id。我尝试{{form.instance.organization}}然后我得到无应该是选择字段

views.py:

class AddNewView(generic.View):
formClass = AddNewForm
template_name = 'myapp/subscription_form.html'

def get(self, request):
    groups = self.request.user.groups.values_list('id', flat=True).first()
    form = self.formClass(groups, None)
    return render(request, self.template_name, {'form': form})

def post(self, request):

    groups = self.request.user.groups.values_list('id', flat=True).first()
    form = self.formClass(groups, request.POST)

    if form.is_valid():
        subscription = form.save(commit=False)

        organization = request.user.groups.values_list('id', flat=True).first()
        input_user = self.request.user
        stuff = form.cleaned_data['stuff']
        description = form.cleaned_data['description']

        subscription.save()

    return render(request, self.template_name, {'form': form})

forms.py:

class AddNewForm(forms.ModelForm):
  def __init__(self, groups,*args,**kwargs):
      super (AddNewView, self ).__init__(*args,**kwargs) 
      self.fields['organization'].queryset = Organization.objects.filter(group=groups)

  class Meta:
      model = Subscription
      fields = [
          'organization',
          'stuff',
          'description',
      ]

models.py:

class Organization(models.Model):
  d_number = models.CharField(max_length=25)
  city = models.CharField(max_length=100)
  group = models.ManyToManyField(Group, help_text="groups")

class Subscription(models.Model):
  organization = models.ForeignKey(Group, help_text="which organization")
  input_user = models.CharField(max_length=150)
  input_date = models.DateTimeField(auto_now_add=True)
  description = models.CharField(max_length=1000, null=True, blank=True, help_text='Description')
  stuff = models.CharField(max_length=100)

模板:

<form action="" method="post">
    {% csrf_token %}
        <!-- Left Inputs -->
        <div class="col-xs-6 wow animated slideInLeft" data-wow-delay=".5s">
            <!-- Organization -->
            {{ form.non_field_errors }}
            <div class="fieldWrapper">
                {{ form.organization.errors }}
                <label>Organization:</label>
                {{ form.organization }}
            </div>

            <!-- stuff -->
            {{ form.non_field_errors }}
            <div class="fieldWrapper">
                {{ form.stuff.errors }}
                <input type="text" name="stuff" id="id_stuff" required placeholder="stuff" class="form"/>
            </div>

        </div><!-- End Left Inputs -->
        <!-- Right Inputs -->
        <div class="col-xs-6 wow animated slideInRight" data-wow-delay=".5s">
            <!-- description -->
            {{ form.non_field_errors }}
            <div class="fieldWrapper">
                <textarea name="description" id="id_description" class="form textarea"  placeholder="description"></textarea>
            </div>
        </div><!-- End Right Inputs -->
        <div class="relative fullwidth col-xs-12">
            <!-- Send Button -->
            <button type="submit" class="form-btn semibold">Vnesi</button>
        </div><!-- End Bottom Submit -->
</form>

0 个答案:

没有答案