Rendering forms in templates

After creating the form, programming the view, and adding the URL pattern, we are only missing the template for this view. Create a new file in the blog/templates/blog/post/ directory and name it share.html; add the following code to it:

{% extends "blog/base.html" %}{% block title %}Share a post{% endblock %}{% block content %}  {% if sent %}    <h1>E-mail successfully sent</h1>    <p>      "{{ post.title }}" was successfully sent to {{ form.cleaned_data.to }}.    </p>  {% else %}    <h1>Share "{{ post.title }}" by e-mail</h1>    <form action="." method="post">      {{ form.as_p }}      {% csrf_token %}      <input type="submit" value="Send e-mail">    </form>  {% endif %}{% endblock %}

This is the template to display the form or a success message when ...

Get Django 2 by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.