views.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. from django.shortcuts import render, redirect
  2. from django.http import HttpResponse, HttpResponseNotFound, Http404, HttpResponseRedirect
  3. from django.urls import reverse
  4. from django.template.loader import render_to_string
  5. from django.template.defaultfilters import slugify
  6. # Create your views here.
  7. menu = [{'title': "О сайте", 'url_name': 'about'},
  8. {'title': "Добавить статью", 'url_name': 'add_page'},
  9. {'title': "Обатная связь", 'url_name': 'contact'},
  10. {'title': "Войти", 'url_name': 'login'}
  11. ]
  12. data_db = [
  13. {'id': 1, 'title': 'Анджелина Джоли', 'content': 'Биография Анджелины Джоли', 'is_published': True },
  14. {'id': 2, 'title': 'Марго Робби', 'content': 'Биография Марго Робби', 'is_published': False },
  15. {'id': 3, 'title': 'Джулия Робертс', 'content': 'Биография Джулия Робертс', 'is_published': True },
  16. ]
  17. def index(request): # HttRequest
  18. # t = render_to_string('women/index.html')
  19. # return HttpResponse(t)
  20. # data = {'title': 'главная страница?',
  21. # 'menu': menu,
  22. # 'float': 234.465,
  23. # 'lst': [1, 2, 'abc', True],
  24. # 'set': {1, 2, 3, 4, 5},
  25. # 'dict': {'key_1': 'value_1', 'key_2': 'value_2'},
  26. # 'obj': MyClass(10, 20),
  27. # 'url': slugify("The main page"),
  28. # }
  29. data = {'title': 'Главная страница',
  30. 'menu': menu,
  31. 'posts': data_db,
  32. }
  33. return render(request, 'women/index.html', context=data)
  34. def about(request):
  35. return render(request, 'women/about.html', {'title': 'О сайте'})
  36. def show_post(request, post_id):
  37. return HttpResponse(f"Отображение статьи с id = {post_id}")
  38. def addpage(request):
  39. return HttpResponse("Добавление статьи")
  40. def contact(request):
  41. return HttpResponse("Обратная связь")
  42. def login(request):
  43. return HttpResponse("Авторизация")
  44. def page_not_found(request, exception):
  45. return HttpResponseNotFound("<h1>Страница не найдена</h1>")