Django侧边栏表格

时间:2011-08-11 20:17:41

标签: python django sidebar

我试图在网站上构建一个侧边栏表格,以便搜索一些类似于http://cl.ly/0e0R1T0G3B1x0c451F22的内容,其中一个人可以在网站的任何部分搜索给定某些参数的内容,这意味着表单必须到处显示,这就是我遇到问题的地方。

似乎视图没有将表单传递到网站的侧边栏,我该怎样做才能始终将空表单作为侧边栏发送。

我想尽可能清楚,但我知道这可能还不够,请让我知道,我要澄清。

这是视图

@render_to(template='league/common_fragments/sidebar_fixturesandresults.html')
def results_fixt_search(request):
results_fixt_search_form = results_fixt_SearchForm(request)

return {'results_fixt_search_form': results_fixt_search_form, }

这是表格,请注意我使用的是django-uni-form

class HorizRadioRenderer(forms.RadioSelect.renderer):
""" this overrides widget method to put radio buttons horizontally
    instead of vertically.
"""
def render(self):
    """Outputs radios"""
    return mark_safe(u'\n'.join([u'%s\n' % w for w in self]))

class results_fixt_SearchForm(forms.Form):

league_search = forms.ChoiceField(choices=[ (league.slug, league.title ) for league in League.objects.all()])
radios = forms.CharField(widget=forms.RadioSelect(renderer=HorizRadioRenderer,
                choices=(('table','Table'),('results','Results'), ('fixtures', 'Fixtures'),)), required=True)


# uniForm Helper
helper = FormHelper()

layout = Layout(
    Fieldset('',
        'league_search', 'radios'
    )
)
helper.add_layout(layout)

# Submit button(s)
submit = Submit('submit','Submit')
helper.add_input(submit)


class Meta:
    fields = ['search_term', 'radios']

def __init__(self, request, *args, **kw):
    super(results_fixt_SearchForm, self).__init__(*args, **kw)
    self.request = request

这是HTML模板

<form action="{% url results_fixt_search %}" method="get" enctype="multipart/form-data" id="results_fixt_search" class="select-form">
    {% with results_fixt_search_form.helper as helper %}
        {% uni_form results_fixt_search_form helper %}
    {% endwith %}
</form>

这是网址

url(r'^(?i)results_fixt/search/$', 'results_fixt_search', {}, name='results_fixt_search'),

1 个答案:

答案 0 :(得分:0)

您可以直接包含表单html(不使用django表单进行渲染):

<form action = '{% url search %}' method='GET'>
    <label>Query: <input type='search' name='query'></label>
    <input type='submit' value='Go'>
</form>

使用上下文处理器将表单实例传递给所有视图:

def search_form_processor(request):
    return {'search_form': SearchForm()}

然后呈现表单:

<form action = '{% url search %}' method='GET'>
    {{ search_form.query.label_tag }} {{ search_form.query }}
    <input type='submit' value='Go'>
</form>

表单可以通过自己的URL在其自己的URL处理。