将AJAX示例从php转换为python / django

时间:2018-08-15 15:27:03

标签: php python django python-3.x

我正在www.w3schools.com上阅读AJAX教程。他们的示例使用php,而我只知道django。我自己完成了大部分转换工作,但非常感谢有人为您提供帮助。

这是php,我认为我可以顺利进入foreach块了:

 <?php
    // Array with names
    $a[] = "Anna";
    $a[] = "Brittany";
    $a[] = "Cinderella";
    $a[] = "Diana";
    $a[] = "Eva";
    $a[] = "Fiona";
    $a[] = "Gunda";
    $a[] = "Hege";
    $a[] = "Inga";
    $a[] = "Johanna";
    $a[] = "Kitty";
    $a[] = "Linda";
    $a[] = "Nina";
    $a[] = "Ophelia";
    $a[] = "Petunia";
    $a[] = "Amanda";
    $a[] = "Raquel";
    $a[] = "Cindy";
    $a[] = "Doris";
    $a[] = "Eve";
    $a[] = "Evita";
    $a[] = "Sunniva";
    $a[] = "Tove";
    $a[] = "Unni";
    $a[] = "Violet";
    $a[] = "Liza";
    $a[] = "Elizabeth";
    $a[] = "Ellen";
    $a[] = "Wenche";
    $a[] = "Vicky";

// get the q parameter from URL
$q = $_REQUEST["q"];

$hint = "";

// lookup all hints from array if $q is different from "" 
if ($q !== "") {
    $q = strtolower($q);
    $len=strlen($q);
    foreach($a as $name) {
        if (stristr($q, substr($name, 0, $len))) {
            if ($hint === "") {
                $hint = $name;
            } else {
                $hint .= ", $name";
            }
        }
    }
}

这是到目前为止我的python代码:

def nameSuggestion(request):
    #list of names
    names = ["Ashley", "Rob", "Becky"]

    #get letters type from URL
    q = request.GET.get("q")

    #will hold name suggestion
    hint = ""

    #lookup all hints from 'names' if hint is not "":
    if q!="":
        q = q.lower()
        q_length = len(q)
        for character #I get confused at this part???

1 个答案:

答案 0 :(得分:1)

我们可以使用生成器来做到这一点:

def nameSuggestion(request):
    a = [
        'Anna', 'Brittany', 'Cinderella', 'Diana', 'Eva', 'Fiona', 'Gunda', 'Hege',
        'Inga', 'Johanna', 'Kitty', 'Linda', 'Nina', 'Ophelia',
        'Petunia', 'Amanda', 'Raquel', 'Cindy', 'Doris', 'Eve',
        'Evita', 'Sunniva', 'Tove', 'Unni', 'Violet', 'Liza',
        'Elizabeth', 'Ellen', 'Wenche', 'Vicky'
    ]
    q = next(filter(None, (
        d.get('q') for d in [request.GET, request.POST, request.COOKIES]
    ), None)
    hint = ''
    if q:
        hint = ', '.join(name for name in a if name.startswith(q))

因此,我们基本上遍历了name中的a,然后检查name是否以我们查询的值(q)开头。如果是的话,我们将其收集起来,并用逗号将它们连在一起。

但是,您仍然需要找到一种构造HTTP响应的方法(您在PHP代码中也不会这样做)。

我认为,如果您想返回JSON,则不想使用这些逗号分隔的值,而是返回JSON格式的列表:

def nameSuggestion(request):
    a = [
        'Anna', 'Brittany', 'Cinderella', 'Diana', 'Eva', 'Fiona', 'Gunda', 'Hege',
        'Inga', 'Johanna', 'Kitty', 'Linda', 'Nina', 'Ophelia',
        'Petunia', 'Amanda', 'Raquel', 'Cindy', 'Doris', 'Eve',
        'Evita', 'Sunniva', 'Tove', 'Unni', 'Violet', 'Liza',
        'Elizabeth', 'Ellen', 'Wenche', 'Vicky'
    ]
    q = next(filter(None, (
        d.get('q') for d in [request.GET, request.POST, request.COOKIES]
    ), None)
    hint = []
    if q:
        hint = [name for name in a if name.startswith(q)]
    return JsonResponse(hint)

此外,您可能可以通过编写例如q = request.GET.get('q')来简化代码。从语义上讲,这与$_REQUEST['q']不同,但是,将GET,POST和COOKIES合并到一起经常被认为是一种糟糕的设计。