Written by: Geoffrey Callaghan

create a contact form with Sphinx

Create A Contact Form With Sphinx

To create a contact form in a Sphinx documentation site using FabForm, you’ll need to follow a few steps. Sphinx is a documentation generator, and FabForm is a form handling service. Here’s how to integrate a contact form into a Sphinx site using FabForm:

Step-by-Step Guide

  1. Set Up Sphinx Project:

    • If you haven’t already, install Sphinx and set up a new Sphinx project:
      pip install sphinx
      sphinx-quickstart
  2. Install a Theme (Optional):

    • You can use a custom theme to improve the appearance of your site. For example, to install the Read the Docs theme:
      pip install sphinx_rtd_theme
    • Update conf.py to use the Read the Docs theme:
      html_theme = 'sphinx_rtd_theme'
  3. Create a Contact Page:

    • Create a new reStructuredText file for your contact page. For example, create a file named contact.rst in your source directory:
      .. _contact:
      
      Contact Us
      ==========
      
      .. raw:: html
      
         <form action="https://fabform.io/f/YOUR_FORM_ID" method="POST">
           <div>
             <label for="name">Name:</label><br>
             <input type="text" id="name" name="name" required><br>
           </div>
           <div>
             <label for="email">Email:</label><br>
             <input type="email" id="email" name="email" required><br>
           </div>
           <div>
             <label for="message">Message:</label><br>
             <textarea id="message" name="message" required></textarea><br>
           </div>
           <div>
             <input type="submit" value="Submit">
           </div>
         </form>
  4. Configure FabForm:

    • Sign up on FabForm.
    • Create a new form to get your form ID.
    • Configure the form settings on FabForm to handle submissions (e.g., set up email notifications, response messages, etc.).
  5. Update Table of Contents:

    • Add the contact page to your Table of Contents (index.rst or contents.rst):
      .. toctree::
         :maxdepth: 2
         :caption: Contents:
      
         contact
  6. Build Your Site:

    • Build your Sphinx site:
      make html
    • Open _build/html/index.html in your web browser and navigate to the contact page to ensure your contact form appears correctly and can submit data.

Example Sphinx Configuration (conf.py):

# Configuration file for the Sphinx documentation builder.

# -- Project information -----------------------------------------------------

project = 'My Project'
copyright = '2024, My Name'
author = 'My Name'

# -- General configuration ---------------------------------------------------

extensions = []

templates_path = ['_templates']
exclude_patterns = []

# -- Options for HTML output -------------------------------------------------

html_theme = 'sphinx_rtd_theme'
html_static_path = ['_static']

Example Contact Page (contact.rst):

.. _contact:

Contact Us
==========

.. raw:: html

   <form action="https://fabform.io/f/YOUR_FORM_ID" method="POST">
     <div>
       <label for="name">Name:</label><br>
       <input type="text" id="name" name="name" required><br>
     </div>
     <div>
       <label for="email">Email:</label><br>
       <input type="email" id="email" name="email" required><br>
     </div>
     <div>
       <label for="message">Message:</label><br>
       <textarea id="message" name="message" required></textarea><br>
     </div>
     <div>
       <input type="submit" value="Submit">
     </div>
   </form>

Example Table of Contents (index.rst):

.. My Project documentation master file, created by
   sphinx-quickstart on Tue Jun 01 2024.
   You can adapt this file completely to your liking, but it should at least
   contain the root `toctree` directive.

Welcome to My Project's documentation!
======================================

.. toctree::
   :maxdepth: 2
   :caption: Contents:

   contact

Indices and tables
==================

* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`

By following these steps, you will have a fully functional contact form on your Sphinx site that integrates with FabForm for handling submissions.