可以在django表单中合并select标签吗?

时间:2016-09-01 12:25:24

标签: javascript html django forms

我有一个django表单在html页面上呈现,我有一个选择下拉菜单,它执行过滤功能。我想知道是否可以将此选择标记作为我表单的一部分?我希望select标签成为routestep_form的一部分。

                  <center>        
                  <form class ="subtitle" method = 'POST' action="{% url 'buildpage:partrequestinfo' %}" enctype="multipart/form-data">{% csrf_token %}
                  {{routestep_form.as_p}}

                  <select name = "myselect" id="id_step" onchange="getOptions(this.value)">
                  <option value="-----">-----</option>
                  {% for step in steps %}
                  <option value="{{ step }}">{{ step }}</option>
                  {%  endfor %}
                  </select>
                  <br/><br/>
                  <select id="id_step_option">
                  <option value="-----">-----</option>
                  </select>

                  <input type='submit' value='Next'/>
                  </form>
                  </center> 

我的表格:

class RouteStepForm(forms.ModelForm):

    class Meta:
        model = RouteStep
        fields = '__all__'
        widgets = {
            'step': Select,
            'step_option': Select,
        }

        error_messages = {
            NON_FIELD_ERRORS: {
                'unique_together': "%(RouteStep)s %(description)s are not unique.",
            }
        }

观点:其他表格是我在同一页面上的表格,我的主要焦点是在RouteStepForm上。

def partrequestinfo(request):
    steps = Step.objects.all()
    if not request.user.is_staff or not request.user.is_superuser:
        raise Http404
    req_form = PartRequestForm(request.POST or None, request.FILES or None)
    step_form = StepForm(request.POST or None, request.FILES or None)
    stepoption_form = StepOptionForm(request.POST or None, request.FILES or None)
    routestep_form = RouteStepForm(request.POST or None, request.FILES or None)

    if req_form.is_valid() and step_form.is_valid() and stepoption_form.is_valid() and routestep_form.is_valid():
            instance = req_form.save(commit=False)
            instance.user = request.user
            instance.save()
            step = step_form.save(commit=False)
            step.save()
            stepoption = stepoption_form.save(commit=False)
            stepoption.save()
            routestep = routestep_form.save(commit=False)
            routestep.save()
            messages.success(request, "Successfully Created")
            return HttpResponseRedirect(instance.get_absolute_url())
    else: 
        context = {
                "req_form": req_form,
                "step_form": step_form,
                "stepoption_form": stepoption_form,
                "routestep_form": routestep_form,
                "steps": steps,
        }

    return render(request, "buildpage/partrequestinfo.html", context)

型号:

class Step(models.Model):
    name = models.CharField(_('Step Name'), max_length=100, default='')
    def __unicode__ (self):
            return self.name

class StepOption(models.Model):
    step = models.ForeignKey(Step, related_name = 'Step', null = True)
    name = models.CharField(_('Step Option'), max_length=100, default='')
    def __unicode__ (self):
            return self.name + " - " + self.step.name

class RouteStep(models.Model):
    step_number = models.PositiveIntegerField(_('Step Number'), default = 0)
    step =  models.ForeignKey(Step, related_name = 'Step+', null = True)    
    step_option = models.ForeignKey(StepOption, related_name = 'StepOption', null=True)
    def __unicode__ (self):
            return self.step_option

1 个答案:

答案 0 :(得分:0)

尝试:

 List<WebElement> listForSize =driver.findElements(By.id("com.readwhere.whitelabel.prabhatkhabar:id/left_drawer_list"));        

    int size = listForSize.size();

    for (int i=0; i< size;i++)       
    { 
        //this is taken again because you are navigate back again
        List<WebElement> list = driver.findElements(By.id("com.readwhere.whitelabel.prabhatkhabar:id/left_drawer_list"));
        System.out.println(list.get(i).getText() + "\n");
        list.get(i).click();
        driver.navigate().back();
        Thread.sleep(2000);//avoid this kind of waiting. wait using until

    }

如果不起作用,请发布显示的模板代码。

修改

我编辑了我的答案,并且发现这有用。Why do I get an object is not iterable error?Getting object not iterable error in django form

另一种解决方案是Can not iterate a ChoiceField with Select as widget