Reputation: 894
I have a form that I want to display.
It looks like this:
# The template to be filled with the form:
# base.html
{% load staticfiles %}
<html>
<head><title>{% block title %}{% endblock %}</title></head>
<body>{% block content %}{% endblock %}</body>
</html>
The concrete template
# home.html
{% extends 'base.html' %}
{% block title %}Main{% endblock %}
{% block content %}
<h2>Home page</h2>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Login</button>
</form>
{% endblock %}
views.py
def home(request):
context = locals()
template = 'home.html'
return render(request, template, context)
urls.py
from django.conf.urls import url
from .views import home as home_view
urlpatterns = [
url(r'^home/$', home_view, name='home'),
]
This does not draw the {{ form.as_p }}
. It only draws the submit button.
Any ideas why?
Upvotes: 1
Views: 67
Reputation: 5029
Your issue stems from the fact that the template has no idea what you are referencing when you use {{ form }}
or {{ form.as_p }}
. The template sees no value associated with the key 'form'
in the context.
To fix this, create a Form
object in your view and then include it in the context when you call render
. Make sure that the key associated with the form in the context dictionary is 'form'
.
Upvotes: 1