Skip to main content

A secure and customizable contact form for Django projects.

Project description

Django Secure Contact Form Plugin

Overview

The Django Secure Contact Form plugin provides a secure and customizable contact form for Django projects. It includes features like CAPTCHA for enhanced security and email notifications.

Installation

To install the plugin, follow these steps:

  1. Install the Package:

    You can install the plugin via PyPI using pip:

    pip install django-secure-contact-form
    
  2. Add the Plugin to Your Django Project:

Add 'django_secure_contact_form' and 'captcha' to your INSTALLED_APPS in settings.py:

    INSTALLED_APPS = [
        # Other installed apps
        'django_secure_contact_form',
        'captcha',
    ]
  1. Include URLs:

In your project's urls.py, include the plugin’s URLs:

from django.urls import include, path
    urlpatterns = [
        # Other URL patterns
        path('contact/', include('django_secure_contact_form.urls')),
        path('captcha/', include('captcha.urls')),
    ]
  1. Apply Migrations: Run the following command to apply the migrations:

    python manage.py migrate

Configuration

Email Configuration

To use the email features of this plugin, configure Django’s email settings in your settings.py file. Here’s an example configuration:

    EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
    EMAIL_HOST = 'smtp.example.com'  # Replace with your SMTP server
    EMAIL_PORT = 587  # Common port for TLS
    EMAIL_USE_TLS = True  # Use TLS if required
    EMAIL_HOST_USER = 'your_email@example.com'  # Replace with your email username
    EMAIL_HOST_PASSWORD = 'your_password'  # Replace with your email password
    DEFAULT_FROM_EMAIL = 'webmaster@example.com'  # Replace with your default email address

CAPTCHA Configuration

Ensure you have the django-simple-captcha library installed and configured:

  1. Install the Library:
    pip install django-simple-captcha
    
  2. Configure CAPTCHA Settings in settings.py:
     CAPTCHA_IMAGE_SIZE = (110, 50)  # Set the size to a larger value if needed
     CAPTCHA_FONT_SIZE = 30           # Increase font size for better readability
     CAPTCHA_LETTER_ROTATION = (-30, 30)  # Adjust rotation
     CAPTCHA_BACKGROUND_COLOR = '#ffffff'  # Background color for contrast
     CAPTCHA_NOISE_FUNCTIONS = (
         'captcha.helpers.noise_arcs',
         'captcha.helpers.noise_dots',
     )
    

Customization

Template Customization

  1. Create a Directory in Your Project’s Templates Folder:

    Create a directory named django_secure_contact_form inside your project's templates folder:

        my_project/
            templates/
                django_secure_contact_form/
    
  2. Copy Templates: Copy the templates from the plugin’s templates directory to this new directory. For example:

    django_secure_contact_form_plugin/
        django_secure_contact_form/
            templates/
                contact_form.html
                contact_success.html
                email_template.html
    

Copy these files to:

my_project/
    templates/
        django_secure_contact_form/
            contact_form.html
            contact_success.html
            email_template.html
  1. Modify Templates:
    Modify the copied templates as needed to match your project’s design and requirements. The files you’ll find are:
  • contact_form.html: The contact form page.
  • contact_success.html: The page displayed after a successful form submission.
  • email_template.html: The template used for the email notifications.

CSS Customization

  1. Create a CSS File: Create a CSS file in your static directory, e.g., static/css/styles.css.

  2. Link the CSS File: Ensure that your contact_form.html links to the CSS file correctly:

    <link rel="stylesheet" type="text/css" href="{% static 'css/styles.css' %}">

  3. Edit the CSS File: Customize the styles as needed. For example:

body {
   font-family: Arial, sans-serif;
   margin: 0;
   padding: 0;
   display: flex;
   justify-content: center;
   align-items: center;
   height: 100vh;
   background-color: #f7f7f7;
}

form {
   background-color: #ffffff;
   padding: 2rem;
   border-radius: 0.5rem;
   box-shadow: 0 0 0.625rem rgba(0, 0, 0, 0.1);
   max-width: 30rem;
   width: 100%;
   margin: 2rem 0;
}

h1 {
   margin-bottom: 1.5rem;
   text-align: center;
}

input[type="text"],
input[type="email"],
textarea {
   width: 100%;
   padding: 0.625rem;
   margin: 0.625rem 0;
   border: 1px solid #cccccc;
   border-radius: 0.25rem;
}

button {
   background-color: #007bff;
   color: #ffffff;
   border: none;
   padding: 0.625rem;
   border-radius: 0.25rem;
   cursor: pointer;
   width: 100%;
}

button:hover {
   background-color: #0056b3;
}

.errors {
   color: red;
   margin: 1rem 0;
}

Usage

  1. Render the Form:

To display the contact form, include the following view in your Django app:

from django.shortcuts import render
from .forms import ContactForm

def contact_form_view(request):
    form = ContactForm()
    return render(request, 'django_secure_contact_form/contact_form.html', {'form': form})
  1. Handle Form Submission:

In your urls.py, map the view to a URL:

from django.urls import path
from .views import contact_form_view

urlpatterns = [
    path('contact/', contact_form_view, name='contact_form'),
]

Testing

  1. Run the Development Server:
python manage.py runserver
  1. Visit the Contact Form Page:

Open your browser and navigate to http://127.0.0.1:8000/contact/ to test the contact form.

  1. Check Email Notifications:

Ensure that the email notifications are sent correctly by submitting the form and verifying the email delivery.

Troubleshooting

  • Static Files Not Loading: Ensure that the static files are correctly collected and served. Run:
python manage.py collectstatic

and ensure that your static files settings in settings.py are configured correctly.

  • Form Not Displaying: Verify that the form is being rendered correctly and check for any template or form-related errors.

  • CAPTCHA Issues: Ensure that django-simple-captcha is installed and configured properly. Check CAPTCHA settings and make sure the CAPTCHA images are being generated and displayed.

Contributing

Feel free to contribute to the project by submitting issues or pull requests on the GitHub repository.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-secure-contact-form-0.2.tar.gz (13.1 kB view details)

Uploaded Source

Built Distribution

django_secure_contact_form-0.2-py3-none-any.whl (15.8 kB view details)

Uploaded Python 3

File details

Details for the file django-secure-contact-form-0.2.tar.gz.

File metadata

File hashes

Hashes for django-secure-contact-form-0.2.tar.gz
Algorithm Hash digest
SHA256 3c50483b68e08aef43a0e2adb51a866d7fc7629a56574812e04c809c4885bf88
MD5 0fd9a05be9b1352b8b61eb2be02b1a2c
BLAKE2b-256 621a8234f64e04151a24a55b8a2ff184ceb266af165b4246cf3f47af96bdc726

See more details on using hashes here.

File details

Details for the file django_secure_contact_form-0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for django_secure_contact_form-0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c32303247b834557da5c232c0dd6cd8df9d951108f22c0f52e7242512c271c46
MD5 b781128e3c63bdfad6da0d11a14bdab1
BLAKE2b-256 1b33731d219984f91e203be2ebacad23ac4cd3725e1a0046888cedb1331e996f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page