Search code examples
pythondjangosidebar

Django sidebar form


Im trying to build a sidebar form on a website in order to search some stuff, kind of like this http://cl.ly/0e0R1T0G3B1x0c451F22 where a person can search for something given certain parameters in any part of the website, meaning that the form must be displayed everywhere, and thats where I have the problem.

it seems that the view is not passing the form to the sidebar on the website, what can I do to always send the empty form as a sidebar.

Im trying to be as clear as possible, yet I know that It might not be enough, please let me know, Ill clarify.

This is the view

@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, }

This is the form, note that Im using 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

this is the HTML template

<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>

And this the URL

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

Solution

  • You can either include the form html directly (without using django forms for rendering):

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

    or pass the form instance to all views using context processor:

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

    and then render the form:

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

    Form may be processed by its own view at its own url then.