Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 895
laike9m Avatar asked Feb 27 '14 06:02

laike9m


People also ask

How do you create a contact form in Python?

Open the contact_form/contact/views.py file and add this code: from django. http import HttpResponse def contact_view(request): return HttpResponse("Contact app works!") When you visit http://127.0.0.1:8000/contact/ in your browser you should see the text “Contact app works!”.

What is forms PY in Django?

forms.py is where the django documentation recommends you place all your forms code; to keep your code easily maintainable. Also, since its a convention mentioned in the documentation, it helps when you are collaborating with others because that is where others will expect to look for your code dealing with forms.

What is get and post method in Django?

GET and POSTDjango's login form is returned using the POST method, in which the browser bundles up the form data, encodes it for transmission, sends it to the server, and then receives back its response. GET , by contrast, bundles the submitted data into a string, and uses this to compose a URL.


1 Answers

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

like image 200
laike9m Avatar answered Oct 28 '22 17:10

laike9m