Chris
Chris

Reputation: 62

Django form in a base class

forms.py

class search_form(forms.Form):
    options = categories.objects.all()
    category = forms.ModelChoiceField(options, initial={'All':'All'}, label='')
    search = forms.CharField(max_length=100, label='', required=False)

This form is used for searching for items. And right now I have it implemented on the index page and it works as expected. The index (home) page has its own view that makes use of this form, but I have a base template (base.html) that every page on the site extends. The base template holds the menu bar and the footer of the site. I need to add the form to the base class and have it function in every template that extends it.

Is there a way to do this?

Upvotes: 0

Views: 93

Answers (1)

NS0
NS0

Reputation: 6096

You can add a custom context processor, which is useful for passing data to every template context, which will give every template access to your form.

As explained in the docs, you need to make a function that will return a dictionary containing your form, and point to it in the settings.

Upvotes: 1

Related Questions