How would I turn this function into a CBV?

瀏覽次數:12 次
跳到第一則未讀訊息

Alexander Joseph

未讀,
2018年6月13日 中午12:48:242018/6/13
收件者:Django users
I found a tutorial for putting forms in modals that works but its not using CBVs and I'd rather use CBVs. Here is the function that works...

def gaas_create(request):
    form = GaasWaferDesignForm()
    context = {'form': form}
    html_form = render_to_string('engineering/gaas_wafer_designs/gaas_wafer_design_form_inner.html',
        context,
        request=request,
    )
    return JsonResponse({'html_form': html_form})

so far this is what I've tried but it doesnt work...

class GaasWaferDesignCreateView(LoginRequiredMixin, CreateView):
    fields = ("design_ui", "emitting", "contact_location", "optical_power", "design_date", "designer", "design_document", "designer_ui", "in_trash", "inactive_date", "notes")
    model = GaasWaferDesign
    template_name = 'engineering/gaas_wafer_designs/gaas_wafer_design_form_inner.html'
    form = GaasWaferDesignForm()
    context = {'form': form}
    html_form = render_to_string('engineering/gaas_wafer_designs/gaas_wafer_design_form_inner.html')

    def options(self, request, *args, **kwargs):
        response = JsonResponse({'html_form': html_form})
        return response

    def form_valid(self, form):
        object = form.save(commit=False)
        object.created_by = self.request.user
        object.save()
        return super(GaasWaferDesignCreateView, self).form_valid(form)


I'm just guessing but i think the part thats causing it not to work might be 

html_form = render_to_string('engineering/gaas_wafer_designs/gaas_wafer_design_form_inner.html',
        context,
        request=request,
    )



Thanks
回覆所有人
回覆作者
轉寄
0 則新訊息