mirror of
https://github.com/DerTyp7/tealcode-django-python.git
synced 2025-10-29 04:22:08 +01:00
added contact form
This commit is contained in:
@@ -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/
|
||||
|
||||
|
||||
@@ -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),
|
||||
]
|
||||
|
||||
0
TealCode/contact/__init__.py
Normal file
0
TealCode/contact/__init__.py
Normal file
5
TealCode/contact/admin.py
Normal file
5
TealCode/contact/admin.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from django.contrib import admin
|
||||
|
||||
from contact.models import Entry
|
||||
|
||||
admin.site.register(Entry)
|
||||
6
TealCode/contact/apps.py
Normal file
6
TealCode/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 = 'contact'
|
||||
7
TealCode/contact/forms.py
Normal file
7
TealCode/contact/forms.py
Normal file
@@ -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']
|
||||
24
TealCode/contact/migrations/0001_initial.py
Normal file
24
TealCode/contact/migrations/0001_initial.py
Normal file
@@ -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)),
|
||||
],
|
||||
),
|
||||
]
|
||||
19
TealCode/contact/migrations/0002_entry_date_created.py
Normal file
19
TealCode/contact/migrations/0002_entry_date_created.py
Normal file
@@ -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),
|
||||
),
|
||||
]
|
||||
0
TealCode/contact/migrations/__init__.py
Normal file
0
TealCode/contact/migrations/__init__.py
Normal file
15
TealCode/contact/models.py
Normal file
15
TealCode/contact/models.py
Normal file
@@ -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
|
||||
|
||||
|
||||
35
TealCode/contact/templates/contact/index.html
Normal file
35
TealCode/contact/templates/contact/index.html
Normal file
@@ -0,0 +1,35 @@
|
||||
{% extends "base.html" %}
|
||||
{% load crispy_forms_tags %}
|
||||
{% block content %}
|
||||
<div class="container">
|
||||
<div class="row text-center pt-3">
|
||||
{% if form %}
|
||||
<h2>Contact Us</h2>
|
||||
{% endif %}
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col"></div>
|
||||
<div class="col-7">
|
||||
<div class="container">
|
||||
{% if form %}
|
||||
<form method="POST" class="form form-dark text-white">
|
||||
{% csrf_token %}
|
||||
{{ form | crispy }}
|
||||
<button type="submit" class="btn btn-primary">Submit</button>
|
||||
<hr>
|
||||
<small class="text-muted">We try to answer you as soon as possible!</small>
|
||||
</form>
|
||||
{% else %}
|
||||
<h2 class="text-center pt-3">Your Message has been sent</h2>
|
||||
<h5 class="text-center pt-3">We try to answer you as soon as possible!</h5>
|
||||
|
||||
<div class="container mx-auto text-center">
|
||||
<button class="btn-outline-success btn mt-5" onclick="window.open('/contact/', '_self');">Back</button>
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
<div class="col"></div>
|
||||
</div>
|
||||
</div>
|
||||
{% endblock content %}
|
||||
3
TealCode/contact/tests.py
Normal file
3
TealCode/contact/tests.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
6
TealCode/contact/urls.py
Normal file
6
TealCode/contact/urls.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from django.urls import path
|
||||
from . import views
|
||||
|
||||
urlpatterns = [
|
||||
path('', views.index, name="contact-index"),
|
||||
]
|
||||
30
TealCode/contact/views.py
Normal file
30
TealCode/contact/views.py
Normal file
@@ -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
|
||||
|
||||
@@ -37,4 +37,21 @@
|
||||
color: white;
|
||||
letter-spacing: 5px;
|
||||
background-color: rgb(36, 126, 121);
|
||||
}
|
||||
}
|
||||
.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;
|
||||
}
|
||||
|
||||
|
||||
@@ -84,6 +84,7 @@
|
||||
<ul class="nav justify-content-center pb-3 mb-3">
|
||||
<li class="nav-item"><a id="a-privacy" href="/privacy/" class="nav-link px-2 text-muted">Cookies/Privacy/Datenschutz</a></li>
|
||||
<li class="nav-item"><a id="a-about"href="/about/" class="nav-link px-2 text-muted">About</a></li>
|
||||
<li class="nav-item"><a id="a-contact"href="/contact/" class="nav-link px-2 text-muted">Contact</a></li>
|
||||
</ul>
|
||||
</footer>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user