def post(self, request, *args, **kwargs): form = CountryCreateFormModel(request.POST) if self.form.is_valid(): country = form.save() return JsonResponse({ "name" : country.name }) return self.get(request, *args, **kwargs)
class CountryCreateView(TemplateView): template_name = "countries/register.html" def dispatch(self, request, *args, **kwargs): self.form = CountryCreateFormModel(request.POST or None) return super().dispatch(request, *args, **kwargs) def get_context_data(self, *args, **kwargs): return {'form': self.form} def post(self, request, *args, **kwargs): if self.form.is_valid(): country = self.form.save() return JsonResponse({'name': country.name}) return self.get(request, *args, **kwargs)
def dispatch(self, request, *args, **kwargs): self.form = CountryCreateFormModel(request.POST or None) return super().dispatch(request, *args, **kwargs)