Form to add slug at homepage

master
grisel-davy 3 years ago
parent 458e0aa846
commit ac1cdd82e3

@ -0,0 +1,4 @@
from django.contrib import admin
from .models import Slug
admin.site.register(Slug)

@ -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'))

@ -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/"),)

@ -0,0 +1,7 @@
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% block content %}
{% crispy form %}
{% endblock %}

@ -1,26 +1,34 @@
{% load bootstrap4 %}
{% load static %}
{% bootstrap_css %}
{% bootstrap_javascript jquery='full' %}
<div class="jumbotron text-center">
<h1>My First Bootstrap Page</h1>
<p>Resize this responsive page to see the effect!</p>
</div>
<html>
<head>
<link rel="stylesheet" type="text/css" href="{% static 'css/base.css' %}"/>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-sm-4">
<h3>Column 1</h3>
<p>Lorem ipsum dolor..</p>
</div>
<div class="col-sm-4">
<h3>Column 2</h3>
<p>Lorem ipsum dolor..</p>
</div>
<div class="col-sm-4">
<h3>Column 3</h3>
<p>Lorem ipsum dolor..</p>
<nav class="navbar navbar-dark bg-dark">
<h1>Litl.</h1>
</nav>
<div class="container">
<div class="row">
<div class="col-sm-1">
</div>
<div class="col-sm-10">
<div class="pt-5">
{% block content %}
{% endblock %}
</div>
</div>
<div class="col-sm-1">
</div>
</div>
</div>
</div>
</div>
</body>
</html>

@ -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"),
]

@ -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})

@ -0,0 +1,7 @@
body{
font-family: Monospace;
}
h1{
color:#FFFFFF;
}
Loading…
Cancel
Save