Solution of 06

This commit is contained in:
2018-12-12 20:14:16 +01:00
parent fccc0f39d6
commit b4ac907a6a
7 changed files with 54 additions and 5 deletions
+9
View File
@@ -0,0 +1,9 @@
from django import forms
import datetime
class NoticeForm(forms.Form):
date_formats = ['%d.%m.%Y', '%d.%m.%y']
title = forms.CharField(label='Titel', max_length=80)
text = forms.CharField(label='Text', max_length=400)
start = forms.DateField(label='Von', input_formats=date_formats, initial=datetime.date.today)
end = forms.DateField(label='Bis', input_formats=date_formats, initial=datetime.date.today)
+5 -2
View File
@@ -3,6 +3,9 @@ from django.urls import path, re_path
urlpatterns = [
# use re_path for regex (.*) - this regex accepts anything - see https://docs.python.org/3/library/re.html for more
path('', views.home, name=''),
re_path('(?P<value>.*)/$', views.home, name='textausgabe')
path('', views.home, name='index'),
re_path('delete/(?P<value>\d+)/$', views.delete, name='delete'),
re_path('(?P<value>.*)/$', views.home, name='textausgabe'),
path('new', views.new, name='new'),
# only accept numbers ^[0-9]*$
]
+22 -2
View File
@@ -1,7 +1,9 @@
from django.shortcuts import render
from django.shortcuts import render, redirect
from datetime import timedelta
from django.utils import timezone
from posts.models import Notice
from posts.forms import NoticeForm
from django.http import HttpResponse
# Create your views here.
@@ -17,4 +19,22 @@ def home(request, value=""):
"title": "Beboop",
"notices": display_notices
}
return render(request, 'index.html', context)
return render(request, 'index.html', context)
def new(request):
if request.method == "POST":
form = NoticeForm(request.POST)
if form.is_valid():
newNotice = Notice(notice_title=form.cleaned_data['title'],
notice_text=form.cleaned_data['text'],
pub_start=form.cleaned_data['start'],
pub_end=form.cleaned_data['end'])
newNotice.save()
return redirect('index')
context = {'form': NoticeForm()}
return render(request, 'edit.html', context)
def delete(request, value):
notice = Notice.objects.get(id = value)
notice.delete()
return redirect('index')