the basic source files are here <https://pastebin.pl/view/a0a1fc38>: 
https://pastebin.pl/view/a0a1fc38  and i am trying to do it in class based 
view for further upgrade. My main project is written in class based view 
and it will be easy to integrate for me.

views.py codes are give below:

from .models import Post,Comment
from .forms import CommentForm
from django.shortcuts import render, get_object_or_404
from django.views.generic import ListView,DetailView
from django.shortcuts import redirect,render, get_object_or_404
from django.http import HttpResponseRedirect

class PostList(ListView):
    model=Post
    template_name='home.html'
    context_object_name='post_list'
    queryset=Post.objects.all()

class PostDetail(DetailView):
    model=Post

    def get_queryset(self):
        queryset = super(PostDetail, self).get_queryset()
        post=Post.objects.all()
        comments=Comment.objects.filter(post=post)
        return queryset

    def get_context_data(self, **kwargs):
        context = super(PostDetail, self).get_context_data(**kwargs)
        if request.method == "POST":
            comment_form = CommentForm(request.POST or None)
            if comment_form.is_valid():
                comment=comment_form.save(commit=False)
                comment.post=post
                comment.save()
        else:
            comment_form = CommentForm()
        context['post']=post.objects.all()
        context['comments']=comments.objects.all()
        context['comment_form']=comment_form()        
        template_name='Post_detail.html'
        return render(request, template_name, context)

when i execute the runserver it shows NameError at /post/1/ name 'request' 
is not defined

the models.py is:

from django.db import models
from django.contrib.auth.models import User

class Post(models.Model):
    title = models.CharField(max_length=200, unique=True)
    author = models.ForeignKey(User, on_delete= 
models.CASCADE,related_name='blog_posts')
    content = models.TextField()

    def __str__(self):
        return self.title

class Comment(models.Model):
    post = 
models.ForeignKey(Post,on_delete=models.CASCADE,related_name='comments')
    name = models.CharField(max_length=80)
    body = models.TextField()
    reply=models.ForeignKey('Comment',on_delete=models.CASCADE,null=True)


    def __str__(self):
        return self.name

the function base view was:

def PostDetail(request,pk):
    post = get_object_or_404(Post, pk=pk)
    comments=Comment.objects.filter(post=post)
    if request.method == "POST":
        comment_form = CommentForm(request.POST or None)
        if comment_form.is_valid():
            comment=comment_form.save(commit=False)
            comment.post=post
            comment.save()
    else:
        comment_form = CommentForm()
    context={
        'post':post,
        'comments':comments,
        'comment_form':comment_form,
    }
    return render(request, 'post_detail.html', context)

that function base view was working fine but i am trying to convert it into 
class based view so that i can use custommixin and other mixin later on. 
how can i make it work?

please let me know. thanx in advance

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/17ba1d3b-8ed2-470f-9004-213357b2d403o%40googlegroups.com.

Reply via email to