From ac1cdd82e35a69bff275c6ec456be4c86cfb9231 Mon Sep 17 00:00:00 2001 From: grisel-davy Date: Fri, 25 Dec 2020 20:15:38 +0100 Subject: [PATCH] Form to add slug at homepage --- litl/admin.py | 4 ++++ litl/forms.py | 20 ++++++++++++++++ litl/settings.py | 4 ++++ litl/templates/add_slug.html | 7 ++++++ litl/templates/base.html | 46 +++++++++++++++++++++--------------- litl/urls.py | 6 +++-- litl/views.py | 18 ++++++++++++++ static/css/base.css | 7 ++++++ 8 files changed, 91 insertions(+), 21 deletions(-) create mode 100644 litl/admin.py create mode 100644 litl/forms.py create mode 100644 litl/templates/add_slug.html create mode 100644 static/css/base.css diff --git a/litl/admin.py b/litl/admin.py new file mode 100644 index 0000000..d0b6dc3 --- /dev/null +++ b/litl/admin.py @@ -0,0 +1,4 @@ +from django.contrib import admin +from .models import Slug + +admin.site.register(Slug) diff --git a/litl/forms.py b/litl/forms.py new file mode 100644 index 0000000..0f3bad3 --- /dev/null +++ b/litl/forms.py @@ -0,0 +1,20 @@ +# coding: utf-8 +# Author: Grizzly +# +# Forms for litl. The main form is for creating the link. + +from django import forms +from crispy_forms.helper import FormHelper +from crispy_forms.layout import Submit +from .models import Slug + +class SlugAddForm(forms.ModelForm): + class Meta: + model = Slug + fields = ('destination','slug') + + def __init__(self,*args,**kwargs): + super().__init__(*args,**kwargs) + self.helper = FormHelper() + self.helper.form_method = 'post' + self.helper.add_input(Submit('submit','Shorten')) diff --git a/litl/settings.py b/litl/settings.py index 9a47c86..b09c9c6 100644 --- a/litl/settings.py +++ b/litl/settings.py @@ -38,6 +38,7 @@ INSTALLED_APPS = [ 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', + 'crispy_forms', 'django.contrib.staticfiles', ] @@ -52,6 +53,7 @@ MIDDLEWARE = [ ] ROOT_URLCONF = 'litl.urls' +CRISPY_TEMPLATE_PACK = 'bootstrap4' TEMPLATES = [ { @@ -120,3 +122,5 @@ USE_TZ = True # https://docs.djangoproject.com/en/3.1/howto/static-files/ STATIC_URL = '/static/' +STATIC_ROOT = os.path.join(BASE_DIR, "static_files") +STATICFILES_DIRS = (os.path.join(BASE_DIR, "static/"),) diff --git a/litl/templates/add_slug.html b/litl/templates/add_slug.html new file mode 100644 index 0000000..830674b --- /dev/null +++ b/litl/templates/add_slug.html @@ -0,0 +1,7 @@ +{% extends 'base.html' %} + +{% load crispy_forms_tags %} + +{% block content %} + {% crispy form %} +{% endblock %} diff --git a/litl/templates/base.html b/litl/templates/base.html index 77c459e..320d292 100644 --- a/litl/templates/base.html +++ b/litl/templates/base.html @@ -1,26 +1,34 @@ {% load bootstrap4 %} +{% load static %} {% bootstrap_css %} {% bootstrap_javascript jquery='full' %} -
-

My First Bootstrap Page

-

Resize this responsive page to see the effect!

-
+ + + + + -
-
-
-

Column 1

-

Lorem ipsum dolor..

-
-
-

Column 2

-

Lorem ipsum dolor..

-
-
-

Column 3

-

Lorem ipsum dolor..

+ + +
+
+
+
+
+
+ {% block content %} + {% endblock %} +
+
+
+
+
-
-
+ + + + diff --git a/litl/urls.py b/litl/urls.py index f199137..eed7122 100644 --- a/litl/urls.py +++ b/litl/urls.py @@ -16,9 +16,11 @@ Including another URLconf from django.contrib import admin from django.urls import path -from .views import index +from .views import index, AddSlug urlpatterns = [ path('admin/', admin.site.urls), - path('', index, name="index"), + #path('', index, name="index"), + path('', AddSlug, name="index"), + ] diff --git a/litl/views.py b/litl/views.py index b4e82fb..129cac4 100644 --- a/litl/views.py +++ b/litl/views.py @@ -4,8 +4,11 @@ # Views for litl. from django.shortcuts import render +from django.http import HttpResponseRedirect from .settings import TEMPLATES +from .models import Slug +from .forms import SlugAddForm def index(request): """Display the home page of the site. @@ -13,3 +16,18 @@ def index(request): """ context = {} return render(request,'base.html',context) + +def AddSlug(request): + """Display the form for creating a new slug. + """ + if request.method == 'POST': + form = SlugAddForm(request.POST) + + if form.is_valid(): + form.save() + return HttpResponseRedirect('') + else: + form = SlugAddForm() + + return render(request,'add_slug.html',{'form':form}) + diff --git a/static/css/base.css b/static/css/base.css new file mode 100644 index 0000000..8dcc8be --- /dev/null +++ b/static/css/base.css @@ -0,0 +1,7 @@ +body{ + font-family: Monospace; +} + +h1{ + color:#FFFFFF; +}