diff --git a/Pipfile b/Pipfile index b55c9de..4dd7aa2 100644 --- a/Pipfile +++ b/Pipfile @@ -4,12 +4,15 @@ verify_ssl = true name = "pypi" [packages] -django = "*" +Django = "*" python-dotenv = "*" -django-phonenumber-field = "*" django-address = "*" +django-crispy-forms = "*" [dev-packages] [requires] python_version = "3.8" + +[packages.django-phonenumber-field] +extras = [ "phonenumbers",] diff --git a/Pipfile.lock b/Pipfile.lock index 2e68b35..d092053 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "008f09930a447935f2210147becbc00e5519c714f2b19b07647972de19557cc4" + "sha256": "57c6fcc08d5613c3832161125af0b9cd702aaa552d0efd5baf912d44aa97d9ab" }, "pipfile-spec": 6, "requires": { @@ -61,6 +61,14 @@ "index": "pypi", "version": "==0.2.5" }, + "django-crispy-forms": { + "hashes": [ + "sha256:35887b8851a931374dd697207a8f56c57a9c5cb9dbf0b9fa54314da5666cea5b", + "sha256:bc4d2037f6de602d39c0bc452ac3029d1f5d65e88458872cc4dbc01c3a400604" + ], + "index": "pypi", + "version": "==1.14.0" + }, "django-phonenumber-field": { "hashes": [ "sha256:897b902a1654b0eb21f6268498a3359e2c4eb90af9585cb8693af186ede8c5bb", @@ -69,6 +77,13 @@ "index": "pypi", "version": "==6.1.0" }, + "phonenumbers": { + "hashes": [ + "sha256:1c8270a2e257d6c65458a42283f82d3eca7f7b9d925454a6966e2f04df75e1cf", + "sha256:386ea186019e8f4d646e48e9da9c3e786c254cfb1bd541cc0a8d0a2ccb85d3ca" + ], + "version": "==8.12.43" + }, "python-dotenv": { "hashes": [ "sha256:32b2bdc1873fd3a3c346da1c6db83d0053c3c62f28f1f38516070c4c8971b1d3", diff --git a/config/settings.py b/config/settings.py index f5114e1..7ed21fb 100644 --- a/config/settings.py +++ b/config/settings.py @@ -26,6 +26,7 @@ BASE_DIR = Path(__file__).resolve().parent.parent # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = os.getenv("SECRET_KEY") +GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY") # SECURITY WARNING: don't run with debug turned on in production! DEBUG = os.getenv("DEBUG", "False") == "True" @@ -49,6 +50,8 @@ INSTALLED_APPS = [ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', + + 'crispy_forms', ] MIDDLEWARE = [ @@ -133,3 +136,11 @@ STATIC_URL = 'static/' # https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' + +CRISPY_TEMPLATE_PACK = 'bootstrap4' + +# Phone numbers + +PHONENUMBER_DB_FORMAT = 'NATIONAL' +PHONENUMBER_DEFAULT_REGION = 'US' +PHONENUMBER_DEFAULT_FORMAT = 'NATIONAL' \ No newline at end of file diff --git a/form/admin.py b/form/admin.py index 8c38f3f..eadbfa2 100644 --- a/form/admin.py +++ b/form/admin.py @@ -1,3 +1,5 @@ from django.contrib import admin +from .models import Response # Register your models here. +admin.site.register(Response) \ No newline at end of file diff --git a/form/forms.py b/form/forms.py index de2b7a5..34e65e7 100644 --- a/form/forms.py +++ b/form/forms.py @@ -1,9 +1,15 @@ -from ast import Mod -import imp -from django.forms import ModelForm +from crispy_forms.helper import FormHelper +from crispy_forms.layout import Submit +from django import forms from .models import Response -class ResponseForm(ModelForm): +class ResponseForm(forms.ModelForm): + helper = FormHelper() + helper.form_method = 'post' + helper.form_action = '' + + helper.add_input(Submit('submit', 'submit')) + class Meta: model = Response - fields = ['name', 'email', 'phone', 'address'] \ No newline at end of file + fields = ['name', 'email', 'phone', 'address'] diff --git a/form/migrations/0001_initial.py b/form/migrations/0001_initial.py new file mode 100644 index 0000000..e22a80a --- /dev/null +++ b/form/migrations/0001_initial.py @@ -0,0 +1,34 @@ +# Generated by Django 4.0.2 on 2022-02-16 01:22 + +import address.models +from django.db import migrations, models +import django.db.models.deletion +import phonenumber_field.modelfields + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ('address', '0004_alter_address_id_alter_country_id_alter_locality_id_and_more'), + ] + + operations = [ + migrations.CreateModel( + name='Response', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=100)), + ('email', models.EmailField(blank=True, max_length=100, null=True)), + ('phone', phonenumber_field.modelfields.PhoneNumberField(blank=True, max_length=128, null=True, region=None)), + ('created_at', models.DateTimeField(auto_now_add=True)), + ('address', address.models.AddressField(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='address.address')), + ], + options={ + 'verbose_name': 'Response', + 'verbose_name_plural': 'Responses', + 'ordering': ['created_at'], + }, + ), + ] diff --git a/form/models.py b/form/models.py index 4cb4fd1..fa358c5 100644 --- a/form/models.py +++ b/form/models.py @@ -1,11 +1,14 @@ from django.db import models +from address.models import AddressField +from phonenumber_field.modelfields import PhoneNumberField # Create your models here. class Response(models.Model): name = models.CharField(max_length=100) - email = models.EmailField(max_length=100) - text = models.TextField() + email = models.EmailField(max_length=100, blank=True, null=True) + phone = PhoneNumberField(blank=True, null=True) + address = AddressField(on_delete=models.CASCADE, blank=True, null=True) created_at = models.DateTimeField(auto_now_add=True) def __str__(self): diff --git a/form/templates/form/base.html b/form/templates/form/base.html index 5c229db..cc14d36 100644 --- a/form/templates/form/base.html +++ b/form/templates/form/base.html @@ -5,6 +5,10 @@ Internet Form + + + + {% block head %}{% endblock %} {% block content %}{% endblock %} diff --git a/form/templates/form/index.html b/form/templates/form/index.html index 09d595e..c014aa8 100644 --- a/form/templates/form/index.html +++ b/form/templates/form/index.html @@ -1,4 +1,36 @@ {% extends 'form/base.html' %} +{% load crispy_forms_tags %} + +{% block head %} +{{ form.media }} + + + +{% endblock %} + {% block content %} -

index.

+
+{% if success %} +

Response submitted. Thank you for your help!

+{% else %} +

Form

+all fields are optional +{% crispy form %} +{% endif %} +
{% endblock %} \ No newline at end of file diff --git a/form/views.py b/form/views.py index 6d53f77..ac86738 100644 --- a/form/views.py +++ b/form/views.py @@ -1,6 +1,21 @@ from django.shortcuts import render +from .forms import ResponseForm # Create your views here. def index(request): - if request.method == 'GET': - return render(request, 'form/index.html') \ No newline at end of file + success = False + + if request.method == 'POST': + form = ResponseForm(request.POST) + if form.is_valid(): + form.save() + success = True + else: + form = ResponseForm() + + context = { + 'form': form, + 'success': success, + } + + return render(request, 'form/index.html', context) \ No newline at end of file