Browse Source

Solution of 06

Praktikum_06_Lsg
Sally Zeitler 5 years ago
parent
commit
b4ac907a6a
7 changed files with 54 additions and 5 deletions
  1. 3
    0
      .gitignore
  2. 1
    1
      MEIM_Lsg/settings.py
  3. 9
    0
      posts/forms.py
  4. 5
    2
      posts/urls.py
  5. 22
    2
      posts/views.py
  6. 12
    0
      templates/edit.html
  7. 2
    0
      templates/index.html

+ 3
- 0
.gitignore View File

*.pyc *.pyc
**__pycache__/ **__pycache__/
local_settings.py local_settings.py
db.sqlite3


.env .env
**/db.sqlite3 **/db.sqlite3


# PyCharm # PyCharm
**/.idea/ **/.idea/



+ 1
- 1
MEIM_Lsg/settings.py View File

# Internationalization # Internationalization
# https://docs.djangoproject.com/en/2.1/topics/i18n/ # https://docs.djangoproject.com/en/2.1/topics/i18n/


LANGUAGE_CODE = 'en-us'
LANGUAGE_CODE = 'de-de'


TIME_ZONE = 'UTC' TIME_ZONE = 'UTC'



+ 9
- 0
posts/forms.py View File

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
posts/urls.py View File



urlpatterns = [ urlpatterns = [
# use re_path for regex (.*) - this regex accepts anything - see https://docs.python.org/3/library/re.html for more # 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
posts/views.py View File

from django.shortcuts import render
from django.shortcuts import render, redirect
from datetime import timedelta from datetime import timedelta
from django.utils import timezone from django.utils import timezone
from posts.models import Notice from posts.models import Notice
from posts.forms import NoticeForm
from django.http import HttpResponse
# Create your views here. # Create your views here.




"title": "Beboop", "title": "Beboop",
"notices": display_notices "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')

+ 12
- 0
templates/edit.html View File

{% extends "base.html" %}

{% block title %} {{ title }} {% endblock title %}

{% block content %}
<h1>Neue Nachricht</h1>
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="save btn btn-default">Speichern</button>
</form>
{% endblock content %}

+ 2
- 0
templates/index.html View File

<div class="jumbotron"> <div class="jumbotron">
<h1 class="display-6">{{ notice.notice_title }}</h1> <h1 class="display-6">{{ notice.notice_title }}</h1>
<p class="lead">{{notice.notice_text}}</p> <p class="lead">{{notice.notice_text}}</p>
<p><a href="{% url 'delete' notice.id %}" class="btn btn-info" role="button">Nachricht löschen</a></p>
</div> </div>
{% endfor %} {% endfor %}
<p><a href="{% url 'new' %}" class="btn btn-info" role="button">Neue Nachricht</a></p>
{% endblock content %} {% endblock content %}

Loading…
Cancel
Save