小编Alb*_*lla的帖子

Django - 在几个模板中"包含"一个块?模板标签?别的什么?

我有一个小的统计数据块,我想在几个地方可用:用户的个人资料页面和一个用户列表的搜索页面.

重复这个区块的最佳方法是什么?我来自PHP背景,在PHP中,它将是一个简单的包含传递一些简单的参数.在django中,我基本上希望能够调用类似于:

 {% stats_block user %}
Run Code Online (Sandbox Code Playgroud)

其中user是包含所有用户信息的对象.我在想一个简单的模板标签,但是这个块非常大,我不想把eveything放在模板标签的一行中.

非常感谢!

django block django-templates

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

更改模型以添加"通过"关系以订购ManytoMany字段 - Django 1.7迁移修改

我正在尝试向我之前创建的ManyToMany字段添加订单.我基本上想订购图片集中的图片.我在Django 1.7上运行,所以没有更多的南迁移(我试图遵循这个教程:http://mounirmesselmeni.github.io/2013/07/28/migrate-django-manytomany-field-to-manytomany-through -with -南/)

这是我的"通过"关系:

class CollectionPictures(models.Model):
    picture = models.ForeignKey(
        Picture,
        verbose_name=u'Picture',
        help_text=u'Picture is included in this collection.',
    )
    collection = models.ForeignKey(
        Collection,
        verbose_name=u'Collection',
        help_text=u'Picture is included in this collection',
    )
    order = models.IntegerField(
        verbose_name=u'Order',
        help_text=u'What order to display this picture within the collection.',
        max_length=255
    )

    class Meta:
        verbose_name = u"Collection Picture"
        verbose_name_plural = u"Collection Pictures"
        ordering = ['order', ]

    def __unicode__(self):
        return self.picture.name + " is displayed in " + self.collection.name + (
        " in …
Run Code Online (Sandbox Code Playgroud)

migration django manytomanyfield django-1.7 django-migrations

10
推荐指数
2
解决办法
5593
查看次数

Django - 向queryset添加字段以存储计算结果

我是Django的新手,来自PHP世界.我正在尝试在计算事物后向查询集添加一个字段,并且不知道该怎么做.在PHP中,我只需在数组中添加一列并将其中的内容存储在其中.

这是我的代码:

def (id):
    mystuff_details    = mystuff_details.objects.filter(stuff_id=id)
    newthing = '';
    for mystuff in mystuff_details:
        newthing_lists = //some query to get another queryset
        for newthing_list in newthing_lists:
            newthing = newthing_list.stuffIwant
            //Here I want to make some computation, and ADD something to newthing, let's say:  
            to_add = (mystuff.score+somethingelse)
            //I've heard about the .append but I'm sure I'm screwing it up
            newthing.append(to_add)
Run Code Online (Sandbox Code Playgroud)

所以基本上在我的模板中,我希望能够致电:{%for newthing in newthings_list%} {{newthing.to_add}} {%end%}

TL; DR:我基本上想从我的数据库中检索一个东西列表,并在这个对象列表中添加一个包含计算值的字段.

让我知道,如果不清楚,我很难从PHP切换到django哈哈.

谢谢!

编辑:

所以,我正在尝试使用dictionnary,但我必须忽略逻辑:

def (id):
    mystuff_details    = mystuff_details.objects.filter(stuff_id=id)
    newthing = {}; …
Run Code Online (Sandbox Code Playgroud)

django list append

8
推荐指数
2
解决办法
2万
查看次数

403使用nginx禁止wordpress索引,其余页面工作正常

我正在一个新的EC2实例上设置我的博客,因为当前托管它的服务器上的一个站点是DDoSed.我在使用nginx时遇到了一些麻烦,因为我可以看到所有页面都很好但索引上有403,或者看到索引但页面上有404(取决于我使用的配置)

这是我的nginx配置:

server {
    listen       80;

    server_name  www.test.com;
    server_name  test.com;
    root /www/blog;

    include conf.d/wordpress/simple.conf;
}
Run Code Online (Sandbox Code Playgroud)

而simple.conf:

location = /favicon.ico {
            log_not_found off;
            access_log off;
    }

    location = /robots.txt {
            allow all;
            log_not_found off;
            access_log off;
    }

    location / {
            # This is cool because no php is touched for static content. 
            # include the "?$args" part so non-default permalinks doesn't break when using query string
            try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
            #NOTE: You should have "cgi.fix_pathinfo = …
Run Code Online (Sandbox Code Playgroud)

wordpress nginx http-status-code-403

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

`perl -ane`,抓住所有参数直到结束(可变数量的args)?

我有一个非常凌乱的perl脚本(我不是Perl大师)就是这个:

perl -ane '($h,$m,$s) = split /:/, $F[0];
           $pid = $F[1];
           $args = $F[2]." ".$F[3]." ".$F[4]." ".$F[5]." ".$F[6]." ".$F[7]." ".$F[8]." ".$F[9]." ".$F[10]." ".$F[11]." ".$F[12]." ".$F[13]; 
    if(($h == 1 && $m > 30) || ($h > 1)) {
        print "$h :: $m $kb $pid\nArguments:\n$args\n\n "; kill 9, $pid }'
Run Code Online (Sandbox Code Playgroud)

我正在寻找一种方法,而不是将所有这些连接用于$arg表示类似的东西$arg=$F[2-end]

我很乐意帮助:)

谢谢!

bash perl

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