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

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

from . import views from . import views


urlpatterns = [ 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

from django.shortcuts import render
import logging
from django.shortcuts import render, redirect, render_to_response
from django.http import HttpResponse from django.http import HttpResponse
from .models import Notice
from django.utils import timezone from django.utils import timezone
import logging
from django.contrib import messages

from .models import Notice
from .forms import NoticeForm


logger = None logger = None


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


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

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

{% 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

{% endblock %} {% endblock %}


{% block content %} {% 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> <div class=container>
{% for notice in notices %} {% 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 %} {% endfor %}

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


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

Loading…
Cancel
Save