我是否需要在视图中使用request.is_ajax()? [英] Do I need to use request.is_ajax() in my views?

查看:64
本文介绍了我是否需要在视图中使用request.is_ajax()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习在Django中使用Ajax,许多教程只是检查 request.method =='GET'还是POST.我很好奇我们接下来需要什么 .is_ajax().是不正常使用它还是教程仅显示基本概念?

I am learning to use Ajax with Django, many tutorials simply check if request.method == 'GET' or POST. I am curious for what do we need .is_ajax() then. Is it normal no to use it or tutorials just show basic concepts?

推荐答案

我很好奇我们那时需要.is_ajax().正常否使用它还是只显示基本概念的教程?

I am curious for what do we need .is_ajax() then. Is it normal no to use it or tutorials just show basic concepts?

是的,不使用 is_ajax 是完全正常的.大多数情况下,您在视图中关心的是HTTP动词(例如GET,POST,PATCH ..).但是,在某些情况下,您想知道该请求是否为AJAX请求.为什么?因为您可能想根据请求是否为Ajax返回不同的结果.

Yes, it is totally normal not to use is_ajax. Most of the time what you care about in your views is the HTTP verb (e.g. GET, POST, PATCH..). However there are certain cases where you want to know if the request is an AJAX request. Why? because you might want to return a different result depending if the request is ajax or not.

此解决方案最常见的用途是PJAX.使用pjax技术时,如果请求不是ajax请求,则呈现整个页面,而如果请求来自ajax,则仅呈现页面的一部分.然后通过某种形式的lib将部分页面添加到网页的正确位置,例如 https://github.com/defunkt/jquery-pjax .

The most common use for this solution is PJAX. When you use a pjax technology, if the request is not an ajax request you render the entire page, whereas if the request comes from ajax you render only a partial of the page. Then the partial page is added in the correct place in the webpage by some sort of lib, such as https://github.com/defunkt/jquery-pjax.

例如,这是我写的在Django中使用Pjax的一种混合方式:

For example, this is a mixing I wrote to use Pjax in django:

import os

from django.views.generic.base import TemplateResponseMixin

class PJAXResponseMixin(TemplateResponseMixin):
    pjax_template_name = None
    pjax_suffix = "pjax"
    pjax_url = True

    def get_context_data(self, **kwargs):
        context = super(TemplateResponseMixin, self).get_context_data(**kwargs)
        context['inner_template'] = self.pjax_template_name
        return context

    def get_template_names(self):
        names = super(PJAXResponseMixin, self).get_template_names()
        if self.request.is_ajax():
            if self.pjax_template_name:
                names = [self.pjax_template_name]
            else:
                names = self._pjaxify_template_var(names)
        return names

    def get(self, request, *args, **kwargs):
        response = super(PJAXResponseMixin, self).get(request, *args, **kwargs)
        if sel

f.pjax_url :
 response['X-PJAX-URL'] = self.request.path
return response

def _pjaxify_template_var(self, template_var):
    if isinstance(template_var, (list, tuple)):
        template_var = type(template_var)(self._pjaxify_template_name(name) for name in template_var)
    elif isinstance(template_var, basestring):
        template_var = self._pjaxify_template_name(template_var)
    return template_var

def _pjaxify_template_name(self, name):
    container = self.request.META.get('HTTP_X_PJAX_CONTAINER', False)
    if container is not False:
        name = _add_suffix(name, clean_container_name(container))
    return _add_suffix(name, self.pjax_suffix)


#################################################
#               HELPER METHODS                  #
#################################################


def clean_container_name(name):
    return name.replace('#', '')


def _add_suffix(name, suffix):
    if "." in name:
        file_name, file_extension = os.path.splitext(name)
        name = "{0}-{1}{2}".format(file_name, suffix, file_extension)
    else:
        name += "-{0}".fomat(suffix)
    return name

基本上,如果请求不是ajax请求,则此混合将呈现默认模板.如果请求是AJAX,则呈现 pjax_template (如果存在),或呈现以 pjax 为前缀的默认模板的名称.

Basically, this mixing renders the default template if the request is not an ajax request. Whereas if the request is AJAX, it renders the pjax_template, if there is one, or the name of the default template prefixed with pjax.

这篇关于我是否需要在视图中使用request.is_ajax()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆