小编mel*_*lis的帖子

在Django中使用TemplateView和ListView有什么区别?

在数据库中,我有一组问题.我想将可折叠项目中的每个问题显示为列表.以前我用的是TemplateView:

class questionmanager(TemplateView):
    template_name = 'questionmanager.html'
    questions = Question.objects.all() 

    def get_context_data(self, **kwargs):
        context = ({
            'questions': self.questions,
        })
        return context  
Run Code Online (Sandbox Code Playgroud)

然后,我读到使用ListView是更好的做法来表示对象列表.然后我改变了我的课程:

class QuestionListView(ListView):
    model = Question

    def get_context_data(self, **kwargs):
        context = super(QuestionListView, self).get_context_data(**kwargs)
        return context
Run Code Online (Sandbox Code Playgroud)

在旧模板中,我使用了这个循环:

{% for question in questions %}
Run Code Online (Sandbox Code Playgroud)

当我使用ListView而不是TemplateView时,我以为我不需要使用for循环; 但我无法列出没有for循环的项目.我在这里找到了一个例子,在我看来,唯一的区别是在for循环中我们使用object_list({% for question in **object_list** %})而不是使用我们在上下文中传递的参数.

我真的没有看到使用TemplateView和ListView之间的这么多差异 - 花了一个小时就可以了.如果有人解释为什么使用ListView而不是TemplateView是一种更好的做法(在这种情况下),我将不胜感激.

提前致谢.

django django-views

7
推荐指数
1
解决办法
2588
查看次数

如何在嵌入式文档数组上使用 $geoNear?

我的文档结构如下:

{ agency_key : '',
  route_id: '',
  direction_id: '',
  stops: [ 
           {stop_id:15, 
            stop_lat: '',
            stop_lon: '',
            loc: [-83.118972, 42, 121567]
            },
            {...
            }
          ]
}
Run Code Online (Sandbox Code Playgroud)

我想在集合中的每个文档的给定距离内找到靠近给定 (lat,lon) 对的停靠点。我创建了二维索引。我尝试了 $unwind,然后是 $geoNear,但它说 $geoNear 只能在管道的第一阶段。我试过这个:

db.tm_stops.aggregate([ 
... { 
...  $geoNear: { near: {coordinates: [-82.958841, 42.370114] },      distanceField: "stops.calculated", query: { agency_key: "DDOT"}, 
... includeLocs: "stops.loc" }
... }
... ])
Run Code Online (Sandbox Code Playgroud)

我尝试了以下方法:

 db.tm_stops.find({stops:{$near:[-82.958841, 42.370114], $maxDistance: 1 } } )
Run Code Online (Sandbox Code Playgroud)

它抛出这个错误:

error: {
    "$err" : "Unable to execute query: error processing query: ns=gtfs.tm_stops limit=0 skip=0\nTree: …
Run Code Online (Sandbox Code Playgroud)

pipeline mongodb node.js aggregation-framework

4
推荐指数
1
解决办法
754
查看次数

Jenkins 中的“容器”关键字是什么?

我最近继承了一些 Jenkins 文件的代码库。在其中之一中,我遇到了这种语法

            stage('Prepare database for integration tests') {
                steps {
                    container('postgres') {
                        sh "..."
                    }
                }
            }
Run Code Online (Sandbox Code Playgroud)

我们的 Jenkins 在 Openshift 上运行,Pod 有多个容器,包括这个 postgres 一个。但是我找不到任何关于使用容器或连接到容器的参考container('containerName')

我试图用以下部分创建一个单独的管道

                        container('az-cli') {
                            try {
                                sh 'ls'
                            } catch (error) {
                                throw error
                            }
                        }
Run Code Online (Sandbox Code Playgroud)

ls列出代码存储库中的内容 - 而不是容器。显然container没有做我认为它做的事情,我找不到任何关于它的文档。有谁知道这是应该做什么?

提前致谢。

jenkins jenkins-declarative-pipeline

2
推荐指数
1
解决办法
1570
查看次数

如何查询 MongoDB 数组中的单个嵌入文档?

我正在尝试查询 MongoDB 数组中的单个嵌入文档。我不知道我做错了什么。以编程方式,我将查询该文档并将新的嵌入文档插入当前的空trips数组中。

{
    "_id" : ObjectId("564b3300953d9d51429163c3"),
    "agency_key" : "DDOT",
    "routes" : [
        {
            "route_id" : "6165",
            "route_type" : "3",
            "trips" : [ ]
        },
        {
            "route_id" : "6170",
            "route_type" : "3",
            "trips" : [ ]
        },
...

    ]
}
Run Code Online (Sandbox Code Playgroud)

以下查询 - 我在 mongo shell 中运行 - 返回空:

db.tm_routes.find( { routes :  {$elemMatch: { route_id:6165 } } } ).pretty();

db.tm_routes.find( { routes :  {$elemMatch: { route_id:6165,route_type:3 } } } ).pretty();

db.tm_routes.find({'routes.route_id':6165}).pretty()
Run Code Online (Sandbox Code Playgroud)

也是。db.tm_routes.find({'routes.route_id':6165}).count()0

以下查询返回数组中的每个文档

db.tm_routes.find({'routes.route_id':'6165'}).pretty();

{ …
Run Code Online (Sandbox Code Playgroud)

arrays mongodb node.js embedded-documents

1
推荐指数
1
解决办法
5074
查看次数