Try to search your question here, if you can't find : Ask Any Question Now ?

How to validate using CBV django

HomeCategory: stackoverflowHow to validate using CBV django
craig asked 3 weeks ago

I am building a pastebin like app. Upto this point most of the basic features are working well.
I wanted to add password feature to the paste but these type CBV examples are not shown in django documentation.
I am using CBV and my detail view is :

class DetailPaste(DetailView):
    template_name = 'pastebin/detail_view.html'

    def get_queryset(self):
        pk = self.kwargs['pk']
        qs = PasteInstance.objects.filter(pk=pk)
        if qs.exists() :
            if qs[0].public_view:
                return qs
            elif self.request.user.is_authenticated:
                return qs
                raise Http404()
            raise Http404()

I know i can define a view using def method procedure but I was wondering if any elegant method is present to do this using CBV.
my model is :

class PasteInstance(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    text = models.TextField(max_length=400)
    timestamp = models.DateTimeField(auto_now_add=True)
    public_view = models.BooleanField(
        default=False, verbose_name='Make this paste public?')
    password = models.CharField(
        max_length=15, blank=True, verbose_name='Protect with password ?')
1 Answers
Best Answer
Fernando answered 3 weeks ago
Your Answer

5 + 13 =

Popular Tags

WP Facebook Auto Publish Powered By :