liloka
liloka

Reputation: 1016

How do I link a form in a Django template?

I'm new to Python and Django so bear with me. I'm trying to include a form section within one base.html class. This is how I've done it:

Views.py:

class WikiForm(forms.Form):
    original = forms.Textarea()
    wikified = forms.Textarea()
    raw_html = forms.Textarea()

def index(request):
    wikiform = WikiForm()
    template = loader.get_template('base.html', wikiform)
    context = RequestContext(request, {})

    return HttpResponse(template.render(context))

Base.html

<div class="sub-background">
    {% block content %}
       {{ wikiForm }}
    {% endblock %}
</div>

This has worked, it's only since trying to add in a form section that it fails with this error.

Any help is much appreciated!

EDIT This is the full error:

> C:\Python27\django-trunk\django\core\handlers\base.py in get_response
                    response = wrapped_callback(request, *callback_args, **callback_kwargs) ...
▶ Local vars
E:\Dropbox\University Project\wikify\Wikify_Project\Wikify_Project\views.py in index
    template = loader.get_template('base.html', wikiform) ...
▶ Local vars
C:\Python27\django-trunk\django\template\loader.py in get_template
    template, origin = find_template(template_name, dirs) ...
▶ Local vars
C:\Python27\django-trunk\django\template\loader.py in find_template
    raise TemplateDoesNotExist(name) ...
▶ Local vars

I can fix this error by not passing in the wikiform into template, but then how do I pass the form into the template to include to render it in the HTML?

Upvotes: 1

Views: 1611

Answers (1)

jproffitt
jproffitt

Reputation: 6355

This line is incorrect:

template = loader.get_template('base.html', wikiform)

According to the development version docs, the structure for this method is this:

get_template(template_name[, dirs])

For django 1.6 and earlier, the dirs parameter does not exist. If you are not using the dev version of django, that line should give you some kind of error about only allowing one parameter. If you are using the dev version, wikiform is not a list of directories, so that would never work.

If you want to pass the form to the template, you need to do this:

wikiform = WikiForm()
template = loader.get_template('base.html')
context = RequestContext(request, {'form': wikiform})

Upvotes: 1

Related Questions