Browse Source

Praktikum 7 Authentifizieren

devel
Heimbs 5 years ago
parent
commit
fbd50e82aa
5 changed files with 89 additions and 11 deletions
  1. 13
    0
      news/posts/forms.py
  2. 3
    1
      news/posts/urls.py
  3. 39
    6
      news/posts/views.py
  4. 18
    0
      news/templates/posts/edit.html
  5. 16
    4
      news/templates/posts/index.html

+ 13
- 0
news/posts/forms.py View File

@@ -0,0 +1,13 @@
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)

+ 3
- 1
news/posts/urls.py View File

@@ -2,5 +2,7 @@ from django.urls import path
from . import views

urlpatterns = [
path('', views.index, name='posts')
path('', views.index, name='posts'),
path('new', views.new, name='new'),
path('delete/<int:deleteId>', views.delete, name='delete',)
]

+ 39
- 6
news/posts/views.py View File

@@ -1,24 +1,57 @@
from django.shortcuts import render
import logging
from django.shortcuts import render, redirect, render_to_response
from django.http import HttpResponse
from .models import Notice
from django.utils import timezone
import logging
from django.contrib import messages

from .models import Notice
from .forms import NoticeForm

logger = None

def init_loger():
def init_loger(logger_type):
global logger
if logger == None:
logger = logging.getLogger('django.db.backends')
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
logger.addHandler(logging.StreamHandler())

# Create your views here.
def index(request):
init_loger()
init_loger('django.db.backends')
notices = Notice.objects.all()
notices = Notice.objects.filter(pub_start__lte=timezone.now(), pub_end__gte=timezone.now())
message = ""

context = {
"notices": notices,
'message': message,
}
return render(request, 'posts/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('posts')
context = {'form' : NoticeForm()}
return render(request, 'posts/edit.html', context)

def delete(request, deleteId=None):
init_loger(__name__)
logger.info(deleteId)
message = ""
if deleteId:
delNotice = Notice.objects.get(id=deleteId)
if delNotice:
delNotice.delete()
message = "Notice has been deleted."
return redirect('posts')

+ 18
- 0
news/templates/posts/edit.html View File

@@ -0,0 +1,18 @@
{% extends 'base.html' %}

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

{% block content %}
<div class=container>
<h1 class="display-4">New Message</h1>

<form method="POST">
<div class="form-group">
{% csrf_token %}{{ form.as_p }}
</div>
<button type="submit" class="save btn btn-default">Speichern</button>
</form>
</div>
{% endblock %}

+ 16
- 4
news/templates/posts/index.html View File

@@ -5,14 +5,26 @@ Index
{% endblock %}

{% block content %}
<div class="jumbotron">
<h1 class="display-4">Index of News App</h1>
<div class=container>
<h1 class="display-4">Your Posts</h1>

<div class=container>
{% for notice in notices %}
<h3>{{ notice.notice_title }}</h3>
<p>{{ notice.notice_text }}</p>
<div class="jumbotron">
<h3>{{ notice.notice_title }}</h3>
<p>{{ notice.notice_text }}</p>
<p><a href="{% url 'delete' deleteId=notice.id %}" class="btn btn-danger" role="button">Delete</a></p>
{% if message %}
<script>
alert('{{ message }}');
</script>
{% endif %}
</div>
{% endfor %}

<div class=container>
<p><a href="{% url 'new' %}" class="btn btn-info" role="button">Neue Nachricht</a></p>
</div>
</div>

<!--p class="lead">Some placeholder Text</p-->

Loading…
Cancel
Save