[Fixed]-Error loading sample django-bootstrap3 template

4πŸ‘

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

πŸ‘€juliocesar

3πŸ‘

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

πŸ‘€besil

1πŸ‘

try this

{# Load the tag library #}
{% load bootstrap3 %}

{# Load CSS and JavaScript #}
{% bootstrap_css %}
{% bootstrap_javascript %}

{# Display django.contrib.messages as Bootstrap alerts #}
{% bootstrap_messages %}

{# Display a form #}
<form action="/url/" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" class="btn btn-primary" value="Submit" />
</form>
πŸ‘€Colo

0πŸ‘

You don’t really need the bootstrap_button tags. I tried to find them as well, but they are not declared in the source…

πŸ‘€Ward

0πŸ‘

Put {% extends 'bootstrap3/bootstrap3.html' %} at the beginning of your snippet. It’s supposed to be your file, bootstrap3.html is at this placeholder.

0πŸ‘

Error is pretty straightforward, make sure you pass valid django form. I was passing form.as_p() instead of form in my view and got this error. Took me a while to notice. May be it will still help somebody.

πŸ‘€Lucas03

-1πŸ‘

for django 1.8 use {{ form }} instead of {{ form.as_p }} as in django 1.6, for this minor change can cause an error

please see django 1.8 official documentation:
https://docs.djangoproject.com/en/1.8/topics/forms/#the-template

{% load bootstrap3 %}
{# Display a form #}

<form action="/submit/" method="post">
    {% csrf_token %}
    {{ form }}
    <input type="submit" value="Submit" />
</form>
πŸ‘€bugsbunny5112

Leave a comment