Search
 
SCRIPT & CODE EXAMPLE
 

BASIC

Detailview with form mixing

from django.shortcuts import render, get_object_or_404, redirect
from django.views.generic.detail import DetailView
from django.views.generic.edit import FormMixin
from .models import Post, Comment
from .forms import CommentForm


class ParticularPost(FormMixin, DetailView):
    template_name='blog/post.html'
    model = Post
    form_class = CommentForm

    def get_success_url(self):
        return reverse('post_detail', kwargs={'pk': self.object.id})

    def get_context_data(self, **kwargs):
        context = super(ParticularPost, self).get_context_data(**kwargs)
        context['form'] = CommentForm(initial={'post': self.object})
        return context

    def post(self, request, *args, **kwargs):
        self.object = self.get_object()
        form = self.get_form()
        if form.is_valid():
            return self.form_valid(form)
        else:
            return self.form_invalid(form)

    def form_valid(self, form):
        form.save()
        return super(ParticularPost, self).form_valid(form)
Comment

PREVIOUS NEXT
Code Example
Basic :: online c++ to c converter 
Basic :: whats up 
Basic :: floppy disk drive +arduino 
Basic :: basic murmur hash function 
Basic :: sequnce function vb.net 
Elixir :: elixir rescue 
Elixir :: elixir get_in 
Elixir :: elixir read csv file 
Elixir :: elixir mapset member 
Elixir :: elixir enum chunk_by 
Scala :: ValueError: If using all scalar values, you must pass an index 
Scala :: foreach batch spark scala 
Scala :: filter by timestamp scala 
Actionscript :: reset udemy course 
Actionscript :: react native uuid 
Excel :: excel get number of column 
Perl :: perl foreach loop 
Perl :: perl make a new directory and change permissions 
Pascal :: subrange variables pascal 
Powershell :: CMD & Powershell History 
Gdscript :: godot progrssbar set max value in code 
Abap :: comments in abap 
Assembly :: flops in deep learning 
Assembly :: dd utility explained examples 
Javascript :: jquery vslidation remove spaces from input 
Javascript :: jquery remove required attribute 
Javascript :: react refresh page 
Javascript :: javascript void(0) href 
Javascript :: get tomorrows date using moment 
Javascript :: random number between 0 and 3 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =