Error loading sample django-bootstrap3 template - django

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.

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

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

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>

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

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

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.

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>

Related

Placement of {% if form.errors %} on login template

I'm not sure if I'm allowed to ask these types of questions here since it's not a problem per say, so please let me know. But I was wondering for the login.html template on Django:
{% extends 'learning_logs/base.html' %}
{% block content %}
{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}
<form method = "post" action="{% url 'login' %}">
{% csrf_token %}
{{ form.as_p }}
<button name = "submit">log in</button>
<input type="hidden" name="next" value="{% url 'learning_logs:index' %}" />
</form>
{% endblock content %}
How come it checks for the form.errors before the it even processes the form? Thanks for any help on this question.
The template ordering doesn't define how the form logic works, only how certain items are displayed. You could put the {% if form.errors %} block after the <form> and there would be no difference to how the form is processed by Django. Furthermore, form.errors will only exist once a form has been validated with is_valid() and any errors exist.

How to solve 'This field is required' for django forms

I created a blog like app. Whenever I update an entry and press the submit button, I want it to take me to view page for that blog entry. However it just redirects me to the update page. When I check form.errors it says that 'This field is required' for all of the fields. But all of the fields are filled with data.
{% extends 'base.html' %}
{% block content %}
{{ form.as_p }}
<form method="POST" action="">
{% csrf_token %}
<input type="submit" value="Update">
</form>
{% endblock content %}
You have your form declared in the wrong block:
{% extends 'base.html' %}
{% block content %}
<form method="POST" action="">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Update">
</form>
{% endblock content %}
Notice that the {{form.as_p}} has to be declared into the form block.
If you fill all data but your form is out of the form tag then the data will not be send.

django-registration-redux and django-crispy-forms: submit button not responding

my registration_form.html is
{% extends '../base.html' %}
{% load crispy_forms_tags %}
{% block content %}
{% crispy form %}
<input type='submit' value="submit">
{% endblock %}
the view is in a module in an existing app. its below:
from registration.views import RegistrationView
from . import forms
class MyRegistrationView(RegistrationView):
def get_form_class(self):
return forms.LoginForm
the url in the urls.py file is
url(r'^accounts/register/$',MyRegistrationView.as_view()),
when the page load, on filling it, when i click submit, it does not submit.
pls what am i missing?
It looks as if your form is not enclosed in a <form> tag. You should have something like:
<form method="post" action="">
{% crispy form %}
<input type='submit' value="submit">
</form>

Django Registration Registration_form

I'm using Django Registration_redux templates for user registrations.
I am wondering how does Django know where to go when user submits his registration when action is action="." ?
{% extends "registration/registration_base.html" %}
{% load i18n %}
{% block title %}{% trans "Register for an account" %}{% endblock %}
{% block content %}
<form method="post" action="">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="{% trans 'Submit' %}" />
</form>
{% endblock %}
{% comment %}
**registration/registration_form.html**
Used to show the form users will fill out to register. By default, has
the following context:
``form``
The registration form. This will be an instance of some subclass
of ``django.forms.Form``; consult `Django's forms documentation
<http://docs.djangoproject.com/en/dev/topics/forms/>`_ for
information on how to display this in a template.
{% endcomment %}
If your action is blank or . then the post goes to the view (URL) which rendered the form.
Here's a simple example using a contact form; https://hellowebapp.com/news/tutorial-setting-up-a-contact-form-with-django/

Page stall when passing data

I am trying to pass some data between pages, but it's not working. Any tips? I click submit and it takes me to a blank page. If I refresh then it shows my base template styles, but no data is passed.
index.html
{% extends "polls/base.html" %}
{% block title %}Vote{% endblock %}
{% block content %}
<h1>Welcome</h1>
<form action="/polls/" method="post">{% csrf_token %}
<p><label for="pin">Enter group pin:</label>
<input id="pin" type="text" name="pin" maxlength="4" />
<input type="submit" value="View Polls" /></p>
</form>
Moderator login
</p>
{% endblock %}
polls/index.html
{% extends "polls/base.html" %}
{% block title %}Recent Polls{% endblock %}
{% block content %}
{{ pin }}
{% endblock %}
polls/urls.py
url(r'^$',
ListView.as_view(
model=Poll,
template_name='polls/index.html')),
You need to write a view to handle the form submision. The view that works, is listing all your Polls. I recommend you to read the tutorial:
Basic views: https://docs.djangoproject.com/en/1.4/intro/tutorial03/
Form submission: https://docs.djangoproject.com/en/1.4/intro/tutorial04/
Basically, your form will send the data to another URL that will handle the processing of your data.
You must specify it in the action attribute:
<form action="/polls/create-poll" method="post">{% csrf_token %}
<input type='text' name='poll-name' />
<input type='submit' />
</form>
and in your views:
def create_poll(request):
poll_name = request.POST.get('poll-name')
poll = Poll.objects.create(name=poll_name)
return HttpResponse("Poll created")
I don't want to sound rude. But you should start with some HTTP and HTML tutorial. A good web programmer is the one that knows the basic stuff in detail. HTTP is a great protocol, try to learn it all the way through.