weixin_33744141 2017-09-10 16:46 采纳率: 0%
浏览 23

Django使用Ajax请求

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'),
]
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥50 用易语言http 访问不了网页
    • ¥50 safari浏览器fetch提交数据后数据丢失问题
    • ¥15 matlab不知道怎么改,求解答!!
    • ¥15 永磁直线电机的电流环pi调不出来
    • ¥15 用stata实现聚类的代码
    • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
    • ¥20 docker里部署springboot项目,访问不到扬声器
    • ¥15 netty整合springboot之后自动重连失效
    • ¥15 悬赏!微信开发者工具报错,求帮改
    • ¥20 wireshark抓不到vlan