How to use django-contact-form (third-party app)?
Asked Answered
T

1

6

django-contact-form is a popular third-party application. It aims to remove tedium and repetition by providing simple, extensible contact-form functionality for Django-powered sites. However I found the documentation is somehow difficult to follow(Perhaps I'm not clever enough:).

After some searching and testing, finally I got it to work. I'll write down the steps and code to help those who might be using it in the future.

Tolmach answered 27/2, 2014 at 6:55 Comment(2)
I'm voting to close this question as off-topic because while this is useful, this isn't really a question. Could you some how reword the question and answer to follow the Stack Exchange format?Actinomycete
@LegoStormtroopr Do you mean I should remove the last paragraph?Tolmach
T
20

1. Install

pip install django-contact-form

2. Add necessary configuration to settings.py

EMAIL_USE_TLS = True  
EMAIL_HOST = 'smtp.gmail.com'  
EMAIL_PORT = 587  
EMAIL_HOST_USER = '[email protected]'  # this is my email address, use yours
EMAIL_HOST_PASSWORD = os.environ['EMAIL_HOST_PASSWORD']   # set environ yourself

ADMINS = (
    ('your_name', 'your_email'),   # email will be sent to your_email
)

MANAGERS = ADMINS

Also, add 'contact_form' to your INSTALLED_APPS.

3. Create contact_form templates

Create a folder called contact_form in your templates folder and add these files into it:

templates  
    └─contact_form  
          contact_form.html  
          contact_form.txt  
          contact_form_sent.html  
          contact_form_subject.txt  

You can write your own, Here's what I use:

contact_form.html

{% extends 'laike9m_blog/blog_base.html' %}

{% block content %}
  <h2>Contact Form</h2>
  <p>To send us a message fill out the below form.</p>
  <form method="post">{% csrf_token %}
    <p>Name: <input type="text" name="name"></p>
    <p>Your e-mail: <input type="text" name="email"></p>
    <p>Message: <textarea name="body" rows="10" cols="50"></textarea></p>
    <input type="submit" value="Submit">
  </form>
{% endblock content %}

contact_form.txt

{{ name }}
{{ email }}
{{ body }} 

contact_form_sent.html

{% extends 'laike9m_blog/blog_base.html' %}

{% block content %}
  <h2>Your message was sent.</h2>
{% endblock content %}

contact_form_subject.txt

message from {{ name }}

4. URLconf

Add this line into your URLconf:

(r'^contact/', include('contact_form.urls')),

All Done

Tolmach answered 27/2, 2014 at 6:55 Comment(7)
What about if i want to have my contact form on main page? (on trailing of the page!)Heelandtoe
@Kasra Eh, I've no idea, sorry. Maybe you can ask the creator for help.Tolmach
@Kasra to embed the form in the main page consider this tutorial: atsoftware.de/2015/02/…. Essentially, your main view should subclass ContactFormView and add the example CustomContactFormView methods such that you're still managing your original main page along with the added contact form functionality. Add the form.py module included in the tutorial as is. Then, all contact form html documents should extend your main page html so that your original content is preserved in addition to the contact form.Haga
@Haga Yeah thanks for suggestion! Now im away from jdngo, I'll try this later thanks!Heelandtoe
Your response should be added to documentation. ThanksGazette
If your application is running on locally sometimes you may need to allow "Allow less secure apps: ON" in your google account app settings (myaccount.google.com/security?pli=1#connectedapps). and sometimes you may need to change EMAIL_HOST_PASSWORD = os.environ['EMAIL_HOST_PASSWORD'] to EMAIL_HOST_PASSWORD = 'your email password'Vermeil
This solution will swallow all the error labels and other Django built-in functionality. Using {{ form.as_p }} in contact_form.html would make more sense.Scorn

© 2022 - 2024 — McMap. All rights reserved.