Lösungen der Aufgaben vom Kurs MEIM1 - Web Engineering von Prof. Hofmann
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

views.py 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import logging
  2. from django.shortcuts import render, redirect, render_to_response
  3. from django.http import HttpResponse
  4. from django.utils import timezone
  5. from django.contrib import messages
  6. from .models import Notice
  7. from .forms import NoticeForm
  8. logger = None
  9. def init_loger(logger_type):
  10. global logger
  11. if logger == None:
  12. logger = logging.getLogger()
  13. logger.setLevel(logging.DEBUG)
  14. logger.addHandler(logging.StreamHandler())
  15. # Create your views here.
  16. def index(request):
  17. init_loger('django.db.backends')
  18. notices = Notice.objects.all()
  19. notices = Notice.objects.filter(pub_start__lte=timezone.now(), pub_end__gte=timezone.now())
  20. message = ""
  21. context = {
  22. "notices": notices,
  23. 'message': message,
  24. }
  25. return render(request, 'posts/index.html', context)
  26. def new(request):
  27. if request.method == "POST":
  28. form = NoticeForm(request.POST)
  29. if form.is_valid():
  30. newNotice = Notice(notice_title=form.cleaned_data['title'],
  31. notice_text=form.cleaned_data['text'],
  32. pub_start=form.cleaned_data['start'],
  33. pub_end=form.cleaned_data['end'])
  34. newNotice.save()
  35. return redirect('posts')
  36. context = {'form' : NoticeForm()}
  37. return render(request, 'posts/edit.html', context)
  38. def delete(request, deleteId=None):
  39. init_loger(__name__)
  40. logger.info(deleteId)
  41. message = ""
  42. if deleteId:
  43. delNotice = Notice.objects.get(id=deleteId)
  44. if delNotice:
  45. delNotice.delete()
  46. message = "Notice has been deleted."
  47. return redirect('posts')