contact form
This commit is contained in:
parent
050cdf637f
commit
e275db6653
11 changed files with 126 additions and 5 deletions
0
app/contact/__init__.py
Normal file
0
app/contact/__init__.py
Normal file
3
app/contact/admin.py
Normal file
3
app/contact/admin.py
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
||||
6
app/contact/apps.py
Normal file
6
app/contact/apps.py
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class ContactConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'app.contact'
|
||||
0
app/contact/migrations/__init__.py
Normal file
0
app/contact/migrations/__init__.py
Normal file
3
app/contact/models.py
Normal file
3
app/contact/models.py
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
from django.db import models
|
||||
|
||||
# Create your models here.
|
||||
3
app/contact/tests.py
Normal file
3
app/contact/tests.py
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
45
app/contact/views.py
Normal file
45
app/contact/views.py
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
import json
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.mail import EmailMessage
|
||||
from django.http import HttpResponse
|
||||
from django.shortcuts import render, redirect, get_object_or_404
|
||||
|
||||
|
||||
def render_to_json_response(dictionary, content_type="application/json; charset=utf-8", status=200):
|
||||
try:
|
||||
response = json.dumps(dictionary, ensure_ascii=False) + '\n'
|
||||
except TypeError:
|
||||
logger.error('failed to serialize to JSON: %s', dictionary)
|
||||
raise
|
||||
if not isinstance(response, bytes):
|
||||
response = response.encode('utf-8')
|
||||
return HttpResponse(response, content_type=content_type, status=status)
|
||||
|
||||
|
||||
def index(request):
|
||||
|
||||
if request.method == 'POST':
|
||||
try:
|
||||
data = json.loads(request.body.decode())
|
||||
except:
|
||||
response = {}
|
||||
return render_to_json_response(response)
|
||||
|
||||
for key in ('email', 'name', 'message'):
|
||||
if not data.get(key):
|
||||
print(key, 'msising', data)
|
||||
return render_to_json_response({})
|
||||
|
||||
name = data['name']
|
||||
email = data['email']
|
||||
message = data['message']
|
||||
subject = '{} has left a message on brixton-timeline'.format(name)
|
||||
from_ = settings.CONTACT_FROM_EMAIL
|
||||
to = settings.CONTACT_TO_EMAIL
|
||||
if not isinstance(to, list):
|
||||
to = [to]
|
||||
msg = EmailMessage(subject, message, from_, to, reply_to=[email])
|
||||
msg.send(fail_silently=True)
|
||||
return render_to_json_response({"sent": True})
|
||||
return redirect('/')
|
||||
Loading…
Add table
Add a link
Reference in a new issue