Best Python code snippet using Kiwi_python
views.py
Source:views.py  
...22        ee = form.save(commit=False)23        ee.user = self.request.user24        form.save()25        return HttpResponseRedirect(self.get_success_url())26    def get_form_kwargs(self):27        kwargs = super().get_form_kwargs()28        kwargs["org_slug"] = self.kwargs["slug"]29        kwargs["user"] = get_user_model()30        return kwargs31    def get_context_data(self, **kwargs):32        context = super().get_context_data()33        context["org"] = Organisation.objects.get(slug=self.kwargs["slug"])34        return context35    def get_success_url(self):36        return reverse_lazy("organisations:detail", args=[self.kwargs["slug"]])37class EngagementEventCreateFromCaf(FormView):38    fields = "__all__"39    form_class = EngagementEventCreateForm40    template_name = "snippets/event_form_base.html"41    def form_valid(self, form):42        ee = form.save(commit=False)43        ee.user = self.request.user44        form.save()45        return HttpResponseRedirect(self.get_success_url())46    def get_form_kwargs(self):47        kwargs = super().get_form_kwargs()48        kwargs["org_slug"] = self.kwargs.get("slug")49        kwargs["user"] = get_user_model()50        kwargs["caf"] = self.kwargs.get("caf_id")51        return kwargs52    def get_context_data(self, **kwargs):53        context = super().get_context_data()54        return context55    def get_success_url(self):56        org_slug = CAF.objects.get(pk=self.kwargs["caf_id"]).organisation.slug57        return reverse_lazy("organisations:detail", args=[org_slug])58class CreateNoteEvent(CreateView):59    form_class = CreateNoteEventForm60    model = NoteEvent61    template_name = "register/create_note_event_form.html"62    def get_form_kwargs(self):63        kwargs = super().get_form_kwargs()64        kwargs["user"] = self.request.user65        return kwargs66    def form_valid(self, form):67        note = form.save(commit=False)68        note.user = self.request.user69        note.save()70        return super().form_valid(form)71    def get_success_url(self):72        return reverse_lazy("organisations:detail", args=[self.object.organisation.slug])73class CreateNoteEventFromOrg(CreateView):74    form_class = CreateNoteEventForm75    model = NoteEvent76    template_name = "register/create_note_event_form.html"77    def get_form_kwargs(self):78        kwargs = super().get_form_kwargs()79        kwargs["user"] = self.request.user80        kwargs["org_slug"] = self.kwargs.get("org_slug")81        return kwargs82    def form_valid(self, form):83        note = form.save(commit=False)84        note.user = self.request.user85        note.save()86        return super().form_valid(form)87class SingleDateTimeEventUpdate(UpdateView):88    model = SingleDateTimeEvent89    fields = [90        "type_descriptor",91        "short_description",92        "date",93        "private",94        "document_link",95        "comments",96        "participants",97        "requested_response_date",98        "response_received_date",99    ]100    template_name_suffix = "_update_form"101    success_url = reverse_lazy("organisations:list")102    def get_success_url(self):103        # We might not have org_slug if we are not setting this from an org detail page104        try:105            self.success_url = reverse_lazy(106                "organisations:detail", args=[self.kwargs["org_slug"]]107            )108            return super().get_success_url()109        except KeyError:110            return super().get_success_url()111class SingleDateTimeEventCreate(FormView):112    template_name = "register/single_datetime_event_create.html"113    form_class = CreateSimpleDateTimeEventForm114    success_url = reverse_lazy("organisations:list")115    def get_success_url(self):116        # We might not have org_slug if we are not setting this from an org detail page117        try:118            self.success_url = reverse_lazy(119                "organisations:detail", args=[self.kwargs["org_slug"]]120            )121            return super().get_success_url()122        except KeyError:123            return super().get_success_url()124    def get_context_data(self, **kwargs):125        context = super().get_context_data()126        if self.kwargs.get("org_slug"):127            context["org"] = Organisation.objects.get(slug=self.kwargs["org_slug"])128            return context129        return context130    def get_form_kwargs(self):131        kwargs = super().get_form_kwargs()132        kwargs["user"] = self.request.user133        kwargs["org_slug"] = self.kwargs.get("org_slug")134        kwargs["event_type"] = self.kwargs.get("event_type")135        return kwargs136    def form_valid(self, form):137        form.save()138        return super().form_valid(form)139class CAFCreateSingleDateEventView(FormView):140    template_name = "register/caf_single_date_event_form.html"141    form_class = CAFSingleDateEventForm142    success_url = reverse_lazy("caf:detail")143    def get_form_kwargs(self):144        kwargs = super().get_form_kwargs()145        kwargs["user"] = self.request.user146        kwargs["caf_id"] = self.kwargs.get("caf_id")147        return kwargs148    def form_valid(self, form):149        form.save()150        return super().form_valid(form)151    def get_success_url(self):152        self.success_url = reverse_lazy("caf:detail", args=[self.kwargs.get("caf_id")])153        return super().get_success_url()154    def get_context_data(self, **kwargs):155        context = super().get_context_data()156        context["caf"] = CAF.objects.get(id=self.kwargs.get("caf_id"))...account.py
Source:account.py  
...9    model=User10    form_class=RegisterForm11    template_name = 'account/register.html'12    success_url='/identity/login/'13    def get_form_kwargs(self):14        kwargs=super().get_form_kwargs()15        kwargs['error_class']=PrettyErrotList16        return kwargs17class CustomLoginView(LoginView):18    template_name = 'account/login.html'19    form_class=CustomLoginForm20    redirect_authenticated_user=True    # ç¨æ·å¨ç»éç¶ææ¶éå®å21    success_url='/'22    def get_form_kwargs(self):23        kwargs=super().get_form_kwargs()24        kwargs['error_class']=PrettyErrotList25        return kwargs26class CustomPasswordChangeView(PasswordChangeView):27    form_class = CustomPasswordChangeForm28    success_url = reverse_lazy('identity:login')29    template_name = 'account/passwordchange.html'30    title = _('Password change')31    def get_form_kwargs(self):32        kwargs = super().get_form_kwargs()33        kwargs['user'] = self.request.user34        kwargs['error_class'] = PrettyErrotList...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
