Law281

Templates/views

Feb 27th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. I'm using python 3.6.0
  2.  
  3. TEMPLATES = [
  4. {
  5. 'BACKEND': 'django.template.backends.django.DjangoTemplates',
  6. 'DIRS': [],
  7. 'APP_DIRS': True,
  8. 'OPTIONS': {
  9. 'context_processors': [
  10. 'django.template.context_processors.debug',
  11. 'django.template.context_processors.request',
  12. 'django.contrib.auth.context_processors.auth',
  13. 'django.contrib.messages.context_processors.messages',
  14. ],
  15. },
  16. },
  17. ]
  18.  
  19. WSGI_APPLICATION = 'cms.wsgi.application'
  20.  
  21. Views
  22. from django.shortcuts import render, get_object_or_404, redirect
  23. from .models import Post, Category
  24.  
  25. from .forms import CommentForm
  26.  
  27.  
  28.  
  29. def list_of_post_by_category(request, category_slug):
  30. categories = Category.objects.all()
  31. post = Post.objects.filter(status='published')
  32. if category_slug:
  33. category = get_object_or_404(Category, slug=category_slug)
  34. post = post.filter(category=category)
  35. template = 'blog/category/list_of_post_by_category.html'
  36. context = {'categories': categories, 'post': post, 'category':category}
  37. return render(request, template, context)
  38.  
  39. def list_of_post(request):
  40. post = Post.objects.filter(status='published')
  41. template = 'blog/post/list_of_post.html'
  42. context = {'post': post}
  43. return render(request, template, context)
  44.  
  45.  
  46. def post_detail(request, slug):
  47. post = get_object_or_404(Post, slug=slug)
  48. template = 'blog/post/post_detail.html'
  49. context = {'post': post}
  50. return render(request, template, context)
  51.  
  52. def add_comment(request, slug):
  53. post = get_object_or_404(Post, slug=slug)
  54. if request.method == 'POST':
  55. form = CommentForm(request.POST)
  56. if form.is_valid():
  57. comment = form.save(commit=False)
  58. comment.post = post
  59. comment.save()
  60. return redirect('blog:post_detail', slug=post.slug)
  61. else:
  62. form = CommentForm()
  63. template = 'blog/post/add_comment.html'
  64. context = {'form': form}
  65. return render(request, template, context)
Advertisement
Add Comment
Please, Sign In to add comment