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.3KB

1 year ago
1234567891011121314151617181920212223242526272829303132333435363738394041
  1. from django.shortcuts import render, redirect
  2. from .models import Notice
  3. from django.utils import timezone
  4. from .forms import NoticeForm
  5. from django.contrib.auth.decorators import login_required
  6. @login_required
  7. def new(request):
  8. if request.method == 'POST':
  9. form = NoticeForm(request.POST)
  10. if form.is_valid():
  11. notice = Notice()
  12. notice.notice_title = form.cleaned_data['notice_title']
  13. notice.notice_text = form.cleaned_data['notice_text']
  14. notice.pub_start = form.cleaned_data['pub_start']
  15. notice.pub_end = form.cleaned_data['pub_end']
  16. notice.save()
  17. return redirect('index')
  18. context = {'form': NoticeForm()}
  19. return render(request, 'polls/edit.html', context)
  20. @login_required
  21. def delete(request, deleteId=None):
  22. if deleteId is not None:
  23. try:
  24. notice = Notice.objects.get(pk=deleteId)
  25. if request.user.id == notice.user_id:
  26. notice.delete()
  27. except:
  28. pass
  29. return redirect('index')
  30. def index(request):
  31. notices = Notice.objects.all()
  32. notices = notices.filter(pub_start__lte=timezone.now())
  33. notices = notices.filter(pub_end__gte=timezone.now())
  34. context = {'notices': notices}
  35. return render(request, 'polls/index.html', context)