I working on a simple mvc application where I have to submit forms with ajax. Then I create a class with purpose to manage all requests from specific module
class Produto(View):
template_name = 'produto/index.html'
def get(self, request):
context = {
'modulos': CELLO_APPS,
'modulo': 'Produtos'
}
return render(request, self.template_name, context)
def post(self, request):
if request.is_ajax() and request.method == "POST":
return JsonResponse({'FOO': 'BAR'})
else:
raise Http404
this looks pretty logical to me, but it doesn't work and raise an error
missing 1 required positional argument: 'request'
the only way I could solve this problem was setting post method as static
class Produto(View):
template_name = 'produto/index.html'
def get(self, request):
context = {
'modulos': CELLO_APPS,
'modulo': 'Produtos'
}
return render(request, self.template_name, context)
def post(request):
if request.is_ajax() and request.method == "POST":
return JsonResponse({'FOO': 'BAR'})
else:
raise Http404
So I have two doubts:
1- How can I create a single class with many functions accessible by ajax?
2- Would be this the best or recommended way to manage those views? (Considering this application can grow a lot in the future)
Edit.
Here follows my urls.py
urlpatterns = [
url(r'^inserir', Produto.inserir, name='produto_inserir'),
url(r'^$', Produto.as_view(), name='cliente'),
]