Reusable forms!

32 views
Skip to first unread message

Alfonso

unread,
Nov 25, 2008, 12:13:09 PM11/25/08
to Django users
Hey,

I've put together a simple search form that works perfectly when
navigating to the corresponding url - '/search.html'. However I want
to reuse that form on multiple pages so where relevant I use {%
include 'search.html' %} to inject the form where I need it.

Problem is this form is devoid of any fields when I do that??

The search.html form:
<div id="col1">
<form action="."method="POST">
{{ form.as_p }}
<p><input type="submit" value="Search"></p>
</form>
</div>

Thanks

Alfonso

Alex Koshelev

unread,
Nov 25, 2008, 12:55:48 PM11/25/08
to django...@googlegroups.com
You have to pass form instance to all pages context where it is used. You can do it explicitly or write custom context processor or (the better solution) write custom inclusion tag.

Alfonso

unread,
Nov 26, 2008, 5:39:05 AM11/26/08
to Django users
Thanks Alex,

I'm attempting to go the inclusion tag route but having difficulty
understanding how to get the form in place correctly.

I've defind a simple form within forms.py:

class PropertySearch(forms.Form):
name = forms.CharField(max_length=100, initial="Property name or
Location")

and within a templatetag folder under that application,
'search_form.py'

from django.template import Library, Node
from pitchup.campsite.forms import PropertySearch
from django import template

register = Library()

def show_form(Search):
form = PropertySearch()
return {'form': form}
register.inclusion_tag('property/property_search.html')(show_form)

But rather predictably that is showing an error... I'm a bit lost, I
understand the inclusion principle for things like 'last 5 blog posts'
etc but including a form seems more difficult?

Thanks

On Nov 25, 5:55 pm, "Alex Koshelev" <daeva...@gmail.com> wrote:
> You have to pass form instance to all pages context where it is used. You
> can do it explicitly or write custom context processor or (the better
> solution) write custom inclusion tag.
>

Alex Koshelev

unread,
Nov 26, 2008, 5:59:29 AM11/26/08
to django...@googlegroups.com
Oh.. and what is the error you get?

Alfonso

unread,
Nov 26, 2008, 6:12:43 AM11/26/08
to Django users
Got it working, it was because I thought I had to pass the seach form
class as an argument to 'show_form'. That wasn't necessary. Taking a
step back from your work does wonders :-)

Thanks Alex

On Nov 26, 10:59 am, "Alex Koshelev" <daeva...@gmail.com> wrote:
> Oh.. and what is the error you get?
>
Reply all
Reply to author
Forward
0 new messages