From b4ac907a6ad4951ebe4fe92ac35e9dfe75c733d3 Mon Sep 17 00:00:00 2001 From: Sally Date: Wed, 12 Dec 2018 20:14:16 +0100 Subject: [PATCH] Solution of 06 --- .gitignore | 3 +++ MEIM_Lsg/settings.py | 2 +- posts/forms.py | 9 +++++++++ posts/urls.py | 7 +++++-- posts/views.py | 24 ++++++++++++++++++++++-- templates/edit.html | 12 ++++++++++++ templates/index.html | 2 ++ 7 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 posts/forms.py create mode 100644 templates/edit.html diff --git a/.gitignore b/.gitignore index 175acde..09f0cbb 100644 --- a/.gitignore +++ b/.gitignore @@ -82,9 +82,12 @@ target/ *.pyc **__pycache__/ local_settings.py +db.sqlite3 .env **/db.sqlite3 # PyCharm **/.idea/ + + diff --git a/MEIM_Lsg/settings.py b/MEIM_Lsg/settings.py index ca52130..424cdd6 100644 --- a/MEIM_Lsg/settings.py +++ b/MEIM_Lsg/settings.py @@ -106,7 +106,7 @@ AUTH_PASSWORD_VALIDATORS = [ # Internationalization # https://docs.djangoproject.com/en/2.1/topics/i18n/ -LANGUAGE_CODE = 'en-us' +LANGUAGE_CODE = 'de-de' TIME_ZONE = 'UTC' diff --git a/posts/forms.py b/posts/forms.py new file mode 100644 index 0000000..0167e3e --- /dev/null +++ b/posts/forms.py @@ -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) \ No newline at end of file diff --git a/posts/urls.py b/posts/urls.py index 13b18f9..ba4c276 100644 --- a/posts/urls.py +++ b/posts/urls.py @@ -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.*)/$', views.home, name='textausgabe') + path('', views.home, name='index'), + re_path('delete/(?P\d+)/$', views.delete, name='delete'), + re_path('(?P.*)/$', views.home, name='textausgabe'), + path('new', views.new, name='new'), + # only accept numbers ^[0-9]*$ ] \ No newline at end of file diff --git a/posts/views.py b/posts/views.py index 803aeb3..acceee4 100644 --- a/posts/views.py +++ b/posts/views.py @@ -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) \ No newline at end of file + 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') \ No newline at end of file diff --git a/templates/edit.html b/templates/edit.html new file mode 100644 index 0000000..db72371 --- /dev/null +++ b/templates/edit.html @@ -0,0 +1,12 @@ +{% extends "base.html" %} + +{% block title %} {{ title }} {% endblock title %} + +{% block content %} +

Neue Nachricht

+
+ {% csrf_token %} + {{ form.as_p }} + +
+{% endblock content %} \ No newline at end of file diff --git a/templates/index.html b/templates/index.html index dba21de..c724584 100644 --- a/templates/index.html +++ b/templates/index.html @@ -7,6 +7,8 @@

{{ notice.notice_title }}

{{notice.notice_text}}

+

Nachricht löschen

{% endfor %} +

Neue Nachricht

{% endblock content %} \ No newline at end of file