1.Create contact_us app by tiping in our terminal :
python manage.py startapp contact_us
2.Copy to models.py this code
from datetime import datetime
class Contact_us(models.Model):
departments = [
(‘NL’, ‘–Not Selected–‘),
(‘SL’, ‘Sales’),
(‘TS’, ‘Technical support’),
(‘MN’, ‘Management’),
]
name = models.CharField(max_length=200)
email = models.EmailField()
phone = models.CharField(max_length=12)
date = models.DateTimeField(default=datetime.now, blank = True)
department = models.CharField(max_length=2, choices=departments, null=True, default=’NL’)
subject = models.CharField(max_length=200)
message = models.TextField()
def __str__(self):
return self.name
3.Add to settings.py this app:
INSTALLED_APPS = [
...
'contact_us.apps.ContactUsConfig',
....
]
4.Create urls.py inside this app and copy this code there:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='contact_us')
]
5.Add to our main urls.py new urlpatter:
`urlpatterns = [
....
path('contact_us/', include('contact_us.urls')),
....
] + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
6.Copy to views.py this code:
from .models import Contact_us
def index(request):
if request.method == ‘POST’:
name = request.POST[‘name’]
email = request.POST[’email’]
phone = request.POST[‘phone’]
subject = request.POST[‘subject’]
department = request.POST[‘department’]
message = request.POST[‘message’]
contact_request = Contact_us(name=name, email=email, phone=phone, subject=subject, department=department, message=message)
#Make notification to admin
recipient = User.objects.get(id=1)
notify.send(recipient, recipient=recipient, verb=’New Contact Us request’)
contact_request.save()
if contact_request.id:
#by defaul using bg-success class
return JsonResponse({‘msg’:’Contact message send’}, safe=False)
else:
return JsonResponse({‘msg’:’Contact message not send’,’class’:’bg-danger’}, safe=False)
return render(request, ‘contact_us/index.html’)
7.Copy to admin.py this code:
from django.contrib import admin
from django_summernote.admin import SummernoteModelAdmin
from .models import Contact_us
class Contact_usAdmin(SummernoteModelAdmin):
summernote_fields = ('message',)
list_display = ('name', 'email', 'phone', 'subject', 'department','date')
list_display_links = ('name', 'email', 'phone')
search_fields = ('department', 'date')
list_per_page = 20
admin.site.register(Contact_us,Contact_usAdmin)
8.Create folder templates/contact_us and index.html inside this new folder. Copy html code from this file:
Add this file to our js folder, and run “python”
function ajax_form(form, callbacks = ‘default_callback’) {
var type = form.getAttribute(‘method’);
var url = form.getAttribute(‘action’);
var formData = new FormData(form);
reg_functions.push(callbacks);
if (reg_functions.length > 0) {
//only fetch data if a function is setup
var r = new XMLHttpRequest();
r.addEventListener(‘readystatechange’, function(event){
if (this.readyState === 4){
if (this.status === 200){
var data = JSON.parse(r.responseText);
reg_functions.forEach(function (func) {
data.form = form;
return window[func](data);
});
}
}
})
r.open(type, url , true);
r.send(formData);
}
return false;
}
function default_callback(data){
var form = data.form;
var msgDiv = form.getElementsByClassName(‘form-msg’);
if(!data.msg){
data.msg=’Form submited’;
}
if(!data.class){
data.class = ‘bga-success’;
}
var node = document.createElement(“span”);
node.innerHTML=data.msg;
if (msgDiv[0].childNodes.length > 1) {
msgDiv[0].removeChild(msgDiv[0].lastChild);
}
msgDiv[0].appendChild(node);
msgDiv[0].classList.add(data.class);
msgDiv[0].style.display = “block”;
form.reset();
}
NOTICE: don’t forget to include this file in our base.html file, simply add: