From f2c9d3216b8db4844770e451db0afa8b1c341f39 Mon Sep 17 00:00:00 2001 From: DerTyp187 Date: Thu, 25 Nov 2021 10:57:40 +0100 Subject: [PATCH] added contact form --- TealCode/TealCode/settings.py | 7 ++++ TealCode/TealCode/urls.py | 1 + TealCode/contact/__init__.py | 0 TealCode/contact/admin.py | 5 +++ TealCode/contact/apps.py | 6 ++++ TealCode/contact/forms.py | 7 ++++ TealCode/contact/migrations/0001_initial.py | 24 +++++++++++++ .../migrations/0002_entry_date_created.py | 19 ++++++++++ TealCode/contact/migrations/__init__.py | 0 TealCode/contact/models.py | 15 ++++++++ TealCode/contact/templates/contact/index.html | 35 +++++++++++++++++++ TealCode/contact/tests.py | 3 ++ TealCode/contact/urls.py | 6 ++++ TealCode/contact/views.py | 30 ++++++++++++++++ TealCode/main/static/css/style.css | 19 +++++++++- TealCode/main/templates/base.html | 1 + 16 files changed, 177 insertions(+), 1 deletion(-) create mode 100644 TealCode/contact/__init__.py create mode 100644 TealCode/contact/admin.py create mode 100644 TealCode/contact/apps.py create mode 100644 TealCode/contact/forms.py create mode 100644 TealCode/contact/migrations/0001_initial.py create mode 100644 TealCode/contact/migrations/0002_entry_date_created.py create mode 100644 TealCode/contact/migrations/__init__.py create mode 100644 TealCode/contact/models.py create mode 100644 TealCode/contact/templates/contact/index.html create mode 100644 TealCode/contact/tests.py create mode 100644 TealCode/contact/urls.py create mode 100644 TealCode/contact/views.py diff --git a/TealCode/TealCode/settings.py b/TealCode/TealCode/settings.py index cb59c68..e3ae54b 100644 --- a/TealCode/TealCode/settings.py +++ b/TealCode/TealCode/settings.py @@ -41,6 +41,9 @@ INSTALLED_APPS = [ 'django.contrib.messages', 'django.contrib.staticfiles', 'main.apps.MainConfig', + 'contact.apps.ContactConfig', + 'crispy_forms', + 'crispy_bootstrap5', ] MIDDLEWARE = [ @@ -129,6 +132,10 @@ USE_L10N = True USE_TZ = True +# Cripsy Forms +CRISPY_ALLOWED_TEMPLATE_PACKS = "bootstrap5" +CRISPY_TEMPLATE_PACK = "bootstrap5" + # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/3.2/howto/static-files/ diff --git a/TealCode/TealCode/urls.py b/TealCode/TealCode/urls.py index f0e7848..afc4986 100644 --- a/TealCode/TealCode/urls.py +++ b/TealCode/TealCode/urls.py @@ -19,6 +19,7 @@ from django.views.generic.base import TemplateView urlpatterns = [ path('', include('main.urls')), + path('contact/', include('contact.urls')), path("robots.txt", TemplateView.as_view(template_name="robots.txt", content_type="text/plain")), path('AD/SDGFOLKJASDNVASDFASDFSLAKDF/', admin.site.urls), ] diff --git a/TealCode/contact/__init__.py b/TealCode/contact/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/TealCode/contact/admin.py b/TealCode/contact/admin.py new file mode 100644 index 0000000..10fab72 --- /dev/null +++ b/TealCode/contact/admin.py @@ -0,0 +1,5 @@ +from django.contrib import admin + +from contact.models import Entry + +admin.site.register(Entry) diff --git a/TealCode/contact/apps.py b/TealCode/contact/apps.py new file mode 100644 index 0000000..002d6c2 --- /dev/null +++ b/TealCode/contact/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class ContactConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'contact' diff --git a/TealCode/contact/forms.py b/TealCode/contact/forms.py new file mode 100644 index 0000000..2ae1a46 --- /dev/null +++ b/TealCode/contact/forms.py @@ -0,0 +1,7 @@ +from django.forms import ModelForm,HiddenInput +from .models import Entry + +class EntryForm(ModelForm): + class Meta: + model = Entry + fields = [ 'email', 'subject', 'message'] \ No newline at end of file diff --git a/TealCode/contact/migrations/0001_initial.py b/TealCode/contact/migrations/0001_initial.py new file mode 100644 index 0000000..814b102 --- /dev/null +++ b/TealCode/contact/migrations/0001_initial.py @@ -0,0 +1,24 @@ +# Generated by Django 3.2.9 on 2021-11-25 08:13 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Entry', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('ip', models.CharField(max_length=80)), + ('email', models.EmailField(max_length=200)), + ('subject', models.CharField(max_length=80)), + ('message', models.TextField(max_length=4000)), + ], + ), + ] diff --git a/TealCode/contact/migrations/0002_entry_date_created.py b/TealCode/contact/migrations/0002_entry_date_created.py new file mode 100644 index 0000000..b1b9ec0 --- /dev/null +++ b/TealCode/contact/migrations/0002_entry_date_created.py @@ -0,0 +1,19 @@ +# Generated by Django 3.2.9 on 2021-11-25 08:14 + +from django.db import migrations, models +import django.utils.timezone + + +class Migration(migrations.Migration): + + dependencies = [ + ('contact', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='entry', + name='date_created', + field=models.DateTimeField(default=django.utils.timezone.now), + ), + ] diff --git a/TealCode/contact/migrations/__init__.py b/TealCode/contact/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/TealCode/contact/models.py b/TealCode/contact/models.py new file mode 100644 index 0000000..f7f6c78 --- /dev/null +++ b/TealCode/contact/models.py @@ -0,0 +1,15 @@ +from django.db import models +from django.utils import timezone + +class Entry(models.Model): + ip = models.CharField(max_length=80) + email = models.EmailField(max_length=200) + subject = models.CharField(max_length=80) + message = models.TextField(max_length=4000) + date_created = models.DateTimeField(default=timezone.now) + + + def __str__(self): + return self.email + " - " + self.subject + + diff --git a/TealCode/contact/templates/contact/index.html b/TealCode/contact/templates/contact/index.html new file mode 100644 index 0000000..bc8eb8d --- /dev/null +++ b/TealCode/contact/templates/contact/index.html @@ -0,0 +1,35 @@ +{% extends "base.html" %} +{% load crispy_forms_tags %} +{% block content %} +
+
+ {% if form %} +

Contact Us

+ {% endif %} +
+
+
+
+
+ {% if form %} +
+ {% csrf_token %} + {{ form | crispy }} + +
+ We try to answer you as soon as possible! +
+ {% else %} +

Your Message has been sent

+
We try to answer you as soon as possible!
+ +
+ +
+ {% endif %} +
+
+
+
+
+{% endblock content %} \ No newline at end of file diff --git a/TealCode/contact/tests.py b/TealCode/contact/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/TealCode/contact/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/TealCode/contact/urls.py b/TealCode/contact/urls.py new file mode 100644 index 0000000..7aa3fca --- /dev/null +++ b/TealCode/contact/urls.py @@ -0,0 +1,6 @@ +from django.urls import path +from . import views + +urlpatterns = [ + path('', views.index, name="contact-index"), +] diff --git a/TealCode/contact/views.py b/TealCode/contact/views.py new file mode 100644 index 0000000..d2f4399 --- /dev/null +++ b/TealCode/contact/views.py @@ -0,0 +1,30 @@ +from django.shortcuts import redirect, render + +from .forms import EntryForm + +def index(req): + if req.method == "POST": + form = EntryForm(req.POST) + if form.is_valid: + entry = form.save() + entry.ip = ip=get_ip(req) + entry.save() + form = False + else: + form = EntryForm() + + context = { + 'current': 'contact', + 'form': form, + } + return render(req, "contact/index.html", context) + + +def get_ip(req): + ip = req.META.get('HTTP_X_REAL_IP') + if ip is None: + return "x.x.x.x" + else: + # We got the client's IP address + return ip + \ No newline at end of file diff --git a/TealCode/main/static/css/style.css b/TealCode/main/static/css/style.css index 0b81ce6..3a7ab1b 100644 --- a/TealCode/main/static/css/style.css +++ b/TealCode/main/static/css/style.css @@ -37,4 +37,21 @@ color: white; letter-spacing: 5px; background-color: rgb(36, 126, 121); -} \ No newline at end of file +} +.form-dark{ + color:white !important; +} +.form-dark input,textarea{ + background-color: rgba(255, 255, 255, 0.089) !important; + color:white !important; + border: 2px solid rgb(27, 41, 35) !important; +} + +.form-dark input:hover,textarea:hover{ + border: 2px solid rgb(61, 138, 102) !important; +} + +.form-dark input:focus,textarea:focus{ + border: 2px solid rgb(61, 138, 102) !important; +} + diff --git a/TealCode/main/templates/base.html b/TealCode/main/templates/base.html index fc2d3f3..53d4d38 100644 --- a/TealCode/main/templates/base.html +++ b/TealCode/main/templates/base.html @@ -84,6 +84,7 @@