Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error loading sample django-bootstrap3 template

I'm still new to Django and Bootstrap so I'm trying out the django-bootstrap package: https://github.com/dyve/django-bootstrap3

The sample template that is included on that page (with a change of url in form action):

{% load bootstrap3 %}

{# Load CSS and JavaScript #}

{% bootstrap_css %}
{% bootstrap_javascript %}

{# Display a form #}

<form action="/search/" method="post" class="form">
        {% csrf_token %}
        {% bootstrap_form form %}
        {% bootstrap_form_buttons %}
                <button type="submit" class="btn btn-primary">
                        {% bootstrap_icon "star" %} Submit
                </button>
        {% end_bootstrap_form_buttons %}
</form>

Gives me the error:

BootstrapError at /
Parameter "form" should contain a valid Django Form.

on this line

{% bootstrap_form form %}

I'm not exactly sure what's the problem is since this is the sample template that's included in that README.

like image 922
dl8 Avatar asked Oct 04 '13 05:10

dl8


2 Answers

{% bootstrap_form form %} is a template tag provided by django-bootstrap3 that expect a django form instance, so the "form" parameter is the context variable mentioned in displaying-a-form-using-a-template from django docs.

Create the form as explained in that page, and then replace the html code they use in template:

<form action="/contact/" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit" />
</form>

by the sample code in your question.

Now Parameter "form" contains a valid Django Form

Hope this helps

like image 97
juliocesar Avatar answered Nov 18 '22 00:11

juliocesar


You just need to provide an object form server side, which must have a context name "form".

In your views.py, include something like this

from django.shortcuts import render

def index(request):
    from django import forms
    class NameForm(forms.Form):
        your_name = forms.CharField(label='Your name', max_length=100)

    template = "your_template.html"
    context = { "form" : NameForm() }
    return render( request, template, context )

Now you should not have any error.

Hope it helps

like image 3
besil Avatar answered Nov 18 '22 00:11

besil