多窗体单视图Django

多窗体单视图Django,django,parameters,django-forms,Django,Parameters,Django Forms,下面是我的场景:我在一个页面上有多个选择框,其中用户从ModelChoiceField中选择选项,并将所选选项作为参数传递到视图。比如: class BrowseForm(forms.Form): thing = forms.ModelChoiceField(queryset=Thing.objects.all(), empty_label=None, widget=forms.Select(attrs={'onchange': 'this.form.submit();'}))

下面是我的场景:我在一个页面上有多个选择框,其中用户从ModelChoiceField中选择选项,并将所选选项作为参数传递到视图。比如:

class BrowseForm(forms.Form):
    thing = forms.ModelChoiceField(queryset=Thing.objects.all(), empty_label=None, widget=forms.Select(attrs={'onchange': 'this.form.submit();'}))
    stuff = forms.ModelChoiceField(queryset=Stuff.objects.all(), empty_label=None, widget=forms.Select(attrs={'onchange': 'this.form.submit();'}))
url(r'^browse/things/(?P<thing>[\w-]+)/$', 'views.browse', name='browse_thing'),
url(r'^browse/stuff/(?P<stuff>[\w-]+)/$', 'views.browse', name='browse_stuff'),
{% for f in form %}
    {% if f.name == 'thing' and has_thing %}
        <!-- render it -->
    {% endif %}
    {% if f.name == 'stuff' and has_stuff %}
        <!-- render it -->
    {% endif %}
{% endfor %}
视图:

这要求我提交
BrowseForm
的两个字段,但我希望
thing
stuff
有两个不同的URL,其中
thing
stuff
作为参数传递,而不是两者都传递。比如:

class BrowseForm(forms.Form):
    thing = forms.ModelChoiceField(queryset=Thing.objects.all(), empty_label=None, widget=forms.Select(attrs={'onchange': 'this.form.submit();'}))
    stuff = forms.ModelChoiceField(queryset=Stuff.objects.all(), empty_label=None, widget=forms.Select(attrs={'onchange': 'this.form.submit();'}))
url(r'^browse/things/(?P<thing>[\w-]+)/$', 'views.browse', name='browse_thing'),
url(r'^browse/stuff/(?P<stuff>[\w-]+)/$', 'views.browse', name='browse_stuff'),
{% for f in form %}
    {% if f.name == 'thing' and has_thing %}
        <!-- render it -->
    {% endif %}
    {% if f.name == 'stuff' and has_stuff %}
        <!-- render it -->
    {% endif %}
{% endfor %}
url(r'^browse/things/(?P[\w-]+)/$,'views.browse',name='browse\u things'),
url(r'^browse/stuff/(?P[\w-]+)/$,'views.browse',name='browse\u stuff'),

有没有一种方法可以用一个表单和一个视图来实现这一点?或者我是否需要为
thing
stuff
编写一个不同的表单,然后检查在我的视图中传递了哪个表单,并相应地重新定向到相应的url?你会怎么做?谢谢你的想法

您可以根据是否传入了
thing
stuff
来选择显示各种元素,因此您的视图将具有:

def browse(request, thing=None, stuff=None):
    # your code, then...
    return render(request, 'browse.html', {
        'form': form,
        'thing_list': thing_list,
        'stuff_list': stuff_list,
        'has_thing': bool(thing),
        'has_stuff': bool(stuff)
    })
然后在模板中,您可以执行以下操作:

class BrowseForm(forms.Form):
    thing = forms.ModelChoiceField(queryset=Thing.objects.all(), empty_label=None, widget=forms.Select(attrs={'onchange': 'this.form.submit();'}))
    stuff = forms.ModelChoiceField(queryset=Stuff.objects.all(), empty_label=None, widget=forms.Select(attrs={'onchange': 'this.form.submit();'}))
url(r'^browse/things/(?P<thing>[\w-]+)/$', 'views.browse', name='browse_thing'),
url(r'^browse/stuff/(?P<stuff>[\w-]+)/$', 'views.browse', name='browse_stuff'),
{% for f in form %}
    {% if f.name == 'thing' and has_thing %}
        <!-- render it -->
    {% endif %}
    {% if f.name == 'stuff' and has_stuff %}
        <!-- render it -->
    {% endif %}
{% endfor %}
{%f格式为%}
{%if f.name=='thing'并具有{u thing%}
{%endif%}
{%if f.name=='stuff'并具有\u stuff%}
{%endif%}
{%endfor%}

还记得在您的
浏览表单
字段中设置
required=False
,这样您就不会在
POST
上出现验证错误,因为您只有一个或另一个。

虽然看起来不是很干燥,但这就是我解决问题的方法:

forms.py:

class StuffBrowseForm(forms.Form):
    stuff = forms.ModelChoiceField(queryset=Stuff.objects.all(), widget=forms.Select(attrs={'onchange': 'this.form.submit();'}))

class ThingBrowseForm(forms.Form):
    thing = forms.ModelChoiceField(queryset=Thing.objects.all(), widget=forms.Select(attrs={'onchange': 'this.form.submit();'}))
视图:

网址:

url(r'^browse/$,'views.browse',name='browse'),
url(r'^browse/stuff/(?P[\w-]+)/$,'views.browse\u stuff',name='browse\u stuff'),
url(r“^browse/things/(?P[\w-]+)/$”,“views.browse\u things”,name='browse\u things'),

路由的正则表达式是完全相同的,那么您如何知道一个是
东西
,另一个是
东西
?我看不出有任何方法可以使用相同的路由实现这一点,您必须以某种方式区分路由,如
r'browse/thing/(?{[\w-]+)/$'
和类似的
东西
。好的,谢谢。我已经相应地更新了帖子。但是在我看来,如何根据表单中选择的选项使用适当的参数重定向?我是否需要多个表单,并根据输入的参数路由到不同的URL?谢谢你的回答,但是我想你可能误解了我的意图。与其基于
东西或
东西显示一个表单,我希望有多个表单,并根据用户是否提交了
东西或
东西重新指向相应的url。有什么想法吗?谢谢!