django
Django - Sending emails using class based view
I have been making a website containing a listview and formview. I have a form.py which looks like this : from django import forms class ContactForm(forms.Form): name = forms.CharField(required=True) email = forms.EmailField(required=True) message = forms.CharField(required=True) and the views.py : from django.shortcuts import render from .forms import ContactForm from django.views.generic import ListView from django.views.generic.edit import FormMixin from blog.models import Post from django.core.mail import EmailMessage class PostListAndFormView(FormMixin,ListView): queryset = Post.objects.all().order_by("-date")[:2] template_name = 'personal/index.html' form_class = ContactForm success_url = 'personal/index.html' This view also deals with a Listview functionality which you can ignore. My HTML template is: <form action="/" method="post"> {% csrf_token %} {{ form }} <input type="submit" value="SEND MESSAGE" /> </form> Finally urls.py : from django.conf.urls import url,include from . import views from homepage.views import PostListAndFormView urlpatterns = [ url(r'^$', PostListAndFormView.as_view(), name='PostListAndFormView'), ] What I want to do here is take the data from the form and send an email to my address with the entered data. Someone help me do this.
Have a look at the documentation, check the methods of the FormMixin and pick the one that fits. form_valid() might be an option. in python3: def form_valid(self, form): # do your stuff # call the parents class method return super().form_valid(form) This is a quite common approach when using class based views. They usually provide a bunch of attributes and methods - sometimes it is enough to just change some attributes, which change the behavior of the methods (e.g. success_url). But often you will need to override some of there methods. It is always a good idea to read their code in this case, cause you need to decide whether you want to call the mixins implementation at the beginning, at the end (as in the example) or if you really want to override it and provide their logic on your own...
Related Links
django-grappelli and chunks app
validate text field and number
Accepting and char in url param
FormWizard: How to proceed from step 1 to Step 2 when having own 2 templates
Django mail sengding error:[ Error 10061]
Error NoReverseMatch on URL
Template design for Django import contacts
Django-allauth: Error: No module named uni_form
How to use DetailView to override the view of an object that does not exists?
Apache 3rd party libraries when running Django
Render django-cms page from other Django app
Django models how to use select_related
Django does assign but not store primary key value
Django, apache2, css not displaying…might be requiring authentication?
What does the proxy_pass option in the NGINX config do?
Django force password expiration