title: Dynamically get template name in bound Django form
body: <p>I am trying to render a Django contact form on any arbitrary page. I am doing it with a request context processor and a template include. This allows me to display the form fine anywhere I want. Then I have a special URL that accepts POST requests (on GET, I just redirect them). If the form is valid, I send an email, and redirect to a success page. On form invalid, I know to pass the form bound with errors, but...I don't know which template to specify because the form is an include and the parent template could be anywhere.</p>
  
  <p>The only way to get something in a Django view is from the request. I can get the path, and with more work, probably the original view from where the POST came from, but that doesn't get me the template.</p>
  
  <pre><code># urls.py
  url(r'^services/$', 'website.views.services', name='services'),
  url(r'^services/contact/$', 'website.views.services_contact', name='services_contact'),
  url(r'^services/contact/done/$', 'website.views.services_contact_done', name='services_contact_done')
  
  
  # views.py
  
  class ServicesView(TemplateView):
      template_name = 'services/services.html'
  
  services = ServicesView.as_view()
  
  class ServicesContactView(View):
  
      def get(self, request, *args, **kwargs):
          return redirect('services')
  
      def post(self, request, *args, **kwargs):
          form = ContactForm(request.POST)
          if form.is_valid():
              form.send_email()
              return redirect('services_contact_done')
          else:
              return render(request, ????, {'contact_form': form})
  
  services_contact = ServicesContactView.as_view()
  
  
  # contact.html
  
  &lt;h2&gt;Contact me&lt;/h2&gt;
  &lt;p&gt;Enter your email to receive your questionnaire&lt;/p&gt;
  
  &lt;form action="{% url 'services_contact' %}" method="post"&gt;
  
      {% csrf_token %}
  
      {% if contact_form.non_field_errors %}
      {{ contact_form.non_field_errors }}
      {% endif %}
  
      {{ contact_form.as_p }}
  
      &lt;button type="submit" name="submit"&gt;Send questionnaire&lt;/button&gt;
  
  &lt;/form&gt;
  
  # home.html
  
  {% extends "base.html" %}
  {% block content %}
  &lt;h1&gt;{{ site.name }}&lt;/h1&gt;
  {% include "services/contact.html" %}
  {% endblock %}
  </code></pre>
  
  <p>The typical Django form view is somewhat silent on form invalid in that its scenario is mostly similar to an unbound form, so it's all just render in the end. My scenario is different due to the template include.</p>