Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Create your views here.
- import operator
- from django.views import generic
- from .models import Blog, BlogAuthor, PostComment, Post
- from django.contrib.auth.models import User # Blog author or commenter
- from django.contrib.auth import login, authenticate
- from django.contrib.auth.forms import UserCreationForm
- from django.shortcuts import render, redirect
- from blog.forms import SignUpForm
- def index(request):
- """
- View function for home page of site.
- """
- # Render the HTML template index.html
- return render(
- request,
- 'index.html',
- )
- def signup(request):
- if request.method == 'POST':
- form = SignUpForm(request.POST)
- if form.is_valid():
- form.save()
- username = form.cleaned_data.get('username')
- raw_password = form.cleaned_data.get('password1')
- user = authenticate(username=username, password=raw_password)
- login(request, user)
- return redirect('index')
- else:
- form = SignUpForm()
- return render(request, 'signup.html', {'form': form})
- class BlogListView(generic.ListView):
- """
- Generic class-based view for a list of all blogs.
- """
- model = Blog
- paginate_by = 5
- class PostListView(generic.ListView):
- model = Post
- paginate_by = 15
- from django.shortcuts import get_object_or_404
- class BlogListbyAuthorView(generic.ListView):
- """
- Generic class-based view for a list of blogs posted by a particular BlogAuthor.
- """
- model = Blog
- paginate_by = 5
- template_name = 'blog/blog_list_by_author.html'
- def get_queryset(self):
- """
- Return list of Blog objects created by BlogAuthor (author id specified in URL)
- """
- id = self.kwargs['pk']
- target_author = get_object_or_404(BlogAuthor, pk=id)
- return Blog.objects.filter(author=target_author)
- def get_context_data(self, **kwargs):
- """
- Add BlogAuthor to context so they can be displayed in the template
- """
- # Call the base implementation first to get a context
- context = super(BlogListbyAuthorView, self).get_context_data(**kwargs)
- # Get the blogger object from the "pk" URL parameter and add it to the context
- context['blogger'] = get_object_or_404(BlogAuthor, pk=self.kwargs['pk'])
- return context
- class PostListY(generic.ListView):
- model = Post
- paginate_by = 15
- template_name = 'blog/post_list.html'
- def get_queryset(self):
- id = self.kwargs['pk']
- target = get_object_or_404(Blog, pk=id)
- return Post.objects.filter(author=target)
- def get_context_data(self, **kwargs):
- # Call the base implementation first to get a context
- context = super(PostListY, self).get_context_data(**kwargs)
- # Get the blogger object from the "pk" URL parameter and add it to the context
- context['blogger'] = get_object_or_404(BlogAuthor, pk=self.kwargs['pk'])
- return context
- class BlogDetailView(generic.DetailView):
- """
- Generic class-based detail view for a blog.
- """
- model = Blog
- template_name = 'blog/post_list.html'
- def get_context_data(self, **kwargs):
- context = super(BlogDetailView,self).get_context_data(**kwargs)
- id = self.kwargs['pk']
- target = get_object_or_404(Blog, pk=id)
- context['post_list'] = Post.objects.filter(blog = target)
- class PostDetailView(generic.DetailView):
- """
- Generic class-based detail view for a post.
- """
- model = Post
- class BloggerListView(generic.ListView):
- """
- Generic class-based view for a list of bloggers.
- """
- model = BlogAuthor
- paginate_by = 5
- from django.contrib.auth.mixins import LoginRequiredMixin
- from django.views.generic.edit import CreateView
- from django.urls import reverse
- class PostCommentCreate(LoginRequiredMixin, CreateView):
- """
- Form for adding a blog comment. Requires login.
- """
- model = PostComment
- fields = ['description', ]
- def get_context_data(self, **kwargs):
- """
- Add associated blog to form template so can display its title in HTML.
- """
- # Call the base implementation first to get a context
- context = super(PostCommentCreate, self).get_context_data(**kwargs)
- # Get the blog from id and add it to the context
- context['post'] = get_object_or_404(Blog, pk=self.kwargs['pk'])
- return context
- def form_valid(self, form):
- """
- Add author and associated blog to form data before setting it as valid (so it is saved to model)
- """
- # Add logged-in user as author of comment
- form.instance.author = self.request.user
- # Associate comment with blog based on passed id
- form.instance.post = get_object_or_404(Post, pk=self.kwargs['pk'])
- # Call super-class form validation behaviour
- return super(PostCommentCreate, self).form_valid(form)
- def get_success_url(self):
- """
- After posting comment return to associated blog.
- """
- return reverse('post-detail', kwargs={'pk': self.kwargs['pk'], })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement