小编thu*_*ief的帖子

在Django中发送大量邮件 - 解压缩的值过多

我正在尝试使用Django的mass_mail函数.下面的代码不断提出"Too Many Values to Unpack"错误,我无法弄清楚原因.我正在关注文件(https://docs.djangoproject.com/en/1.5/topics/email/#send-mass-mail),这看起来非常简单 - 我做错了什么?如果重要的话,发送电子邮件地址已经完成,但我看不出这件事.

if matching_record.level == 1:
                users = self._get_users_to_be_notified(matching_record.category)
                email_recipients = [str(user.email) for user in users if user.email]
                message = 'Here is your requested notification that the service "%s" is having technical difficulties and has been set to "Critical".' %matching_record.name
                mail_tuple = ('Notification', 
                              message, 
                              'notifications@service.com', 
                              email_recipients)
                send_mass_mail(mail_tuple)
Run Code Online (Sandbox Code Playgroud)

python django

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

继承和修改`Meta`类

所以我有一个基本的 ItemTable,然后是从它继承的许多表。我似乎无法修改 Meta 类。我尝试正常地包含元类,但它不起作用,然后我找到了这个错误报告并在下面实现了它。它会默默地失败:表仅使用父元类中的列进行渲染。

class ItemTable(tables.Table):

    class Meta:
        model = Item
        attrs = {"class":"paleblue"}
        fields = ('name', 'primary_tech', 'primary_biz', 'backup_tech', 'backup_biz')

class ApplicationTable(ItemTable):

    def __init__(self, *args, **kwargs):
        super(ApplicationTable, self).__init__(*args, **kwargs)

    class Meta(ItemTable.Meta):
        model = Application
        fields += ('jira_bucket_name',)
Run Code Online (Sandbox Code Playgroud)

编辑:代码修改如下所示。我现在得到一个fields未定义的 NameError 。

python django django-tables2

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

在第三方Django应用程序中自定义模板

我在我的Django项目中使用第三方应用程序(django-social-share),但我需要自定义模板.我不知道该怎么做 - 我尝试的一切都使用默认模板.

当前的默认模板存储在:

django-social-share/django_social_share/templates/django_social_share/templatetags/post_to_facebook.html. 
Run Code Online (Sandbox Code Playgroud)

我已经定制了一个:

{{project_root}}/{{app_name}}/templates/django_social_share/templatetags/post_to_facebook.html
Run Code Online (Sandbox Code Playgroud)

我的模板设置:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'django.core.context_processors.request',
                'mainsite.context_processors.google_api'
            ],
            'loaders': (
                'django.template.loaders.filesystem.Loader',
                'django.template.loaders.app_directories.Loader',
                'django.template.loaders.eggs.Loader',
            ),
        },
    },
]
Run Code Online (Sandbox Code Playgroud)

python django

3
推荐指数
2
解决办法
1393
查看次数

django测试客户端针对所有网址获取404

我正在进行Django测试的第一个实验,但是我遇到的问题是,无论我使用哪个url(甚至/),我总是会得到404模板。如果我将完全相同的代码放入django shell中,则它将按预期工作,并始终向我显示请求的URL的内容。

class SimpleTest(TestCase):
    def setUp(self):
        self.user = User.objects.create_user('test', 'test', 'test')
        self.user.is_staff = True 
        self.user.save()
        self.client = Client()

    def test_something(self):
        self.assertTrue(self.client.login(username='test', password= 'test'))
        self.client.get("/")
Run Code Online (Sandbox Code Playgroud)

登录返回True,但是get()失败。有什么暗示我在这里做错了吗?

django django-testing

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

将 Sublime 与 virtualenv 结合使用

所以,我通读了Using virtualenv with sublime text 2 的所有内容,但看到接受的答案显然是不正确的,我仍然很困惑。

我正在尝试chorus.py从 Sublime 中运行我的文件。我已将构建系统更改为如下所示:

"build_systems":
[
    {
        "name": "Scraper",
        "cmd" : ["/Users/thumbtackthief/.virtualenvs/chorus", "$file"]
    }
]
Run Code Online (Sandbox Code Playgroud)

(基于这样一个事实,当我os.environ['VIRTUAL_ENV']从终端进入时,那是吐出的路径——也许我做错了?)

当我构建我的文件时,我得到[Errno 13] Permission denied. 我不知道从这里去哪里。

python virtualenv sublimetext2

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

Django Model IntegrityError:NOT NULL约束失败:

我正在建立一个使URL简短的服务。我有以下模型:

from django.db import models

class ShortURL(models.Model):
    url = models.CharField(max_length = 50)

class LongURL(models.Model):
    name = models.CharField(max_length = 100, null=True)
    url_to_short = models.ForeignKey(ShortURL)
Run Code Online (Sandbox Code Playgroud)

我已经运行了以下命令:python manage.py migrate 如果打开解释器,请使用python manage.py shell并运行以下代码:

>>> from appshort.models import LongURL
>>> a = LongURL(name = 'hello_long_link')
>>> a.save()
Run Code Online (Sandbox Code Playgroud)

然后我得到错误:

django.db.utils.IntegrityError: NOT NULL constraint failed: appshort_longurl.url_to_short_id

我做错什么了?

python django

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

使用 Swift 以编程方式更改 UIButton 的大小

我知道以前有人问过这个问题,但我似乎无法使任何解决方案起作用。我的按钮设置了 AutoLayout,但为了让它们适合 iPhone 4,我需要让它们更短。

我有这个,我在 viewDidLoad、viewDidAppear 和 viewDidLayoutSubviews 中尝试过(我真的不在乎它在哪里,只要它有效)

for button in choiceButtons! {
    if smallScreen {
        button.frame = CGRect(x: button.frame.origin.x, y: button.frame.origin.y, width: button.frame.width, height: 15)
        println("small")
    } else {
        button.frame = CGRect(x: button.frame.origin.x, y: button.frame.origin.y, width: button.frame.width, height: 55)
        button.frame.size.height = 55 //second attempt (what I'd really like to work)
        print("big")
    }
}
Run Code Online (Sandbox Code Playgroud)

打印语句正确执行,所以我知道这是有效的。我已经检查了所有四个按钮的“在构建时删除占位符”选项。我已经尝试完全从 AutoLayout 中的按钮中删除高度约束(这无关紧要,我只是想防止 XCode 对我大喊大叫)。如果没有其他选择,完全以编程方式创建所有四个按钮似乎很乏味但可行,但我觉得我想做的事情应该不会太难。对于它的价值,如果我将完全相同的代码应用于图像,它就可以工作。只是不是我的按钮。

如何使用 Swift 更改按钮的高度?

无法以编程方式更改 UIButton 的框架。?

uibutton ios swift

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

如何显示prefetch_related生成的查询

我有一个带有prefetch_related()(带Prefetch对象)的QuerySet

我希望看到原始查询,print(qs.query)并且它没有显示任何有关这些prefetch_related内容的信息.

如何查看将要运行的查询prefetch_related

django django-queryset

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

如何遍历一个列表的值,使用函数进行更改并将其添加到第二个列表?

我有以下温度列表:

temp_data =  [19, 21, 21, 21, 23, 23, 23, 21, 19, 21, 19, 21, 23, 27, 27, 28, 30, 30, 32, 32, 32, 32, 34, 34,
         34, 36, 36, 36, 36, 36, 36, 34, 34, 34, 34, 34, 34, 32, 30, 30, 30, 28, 28, 27, 27, 27, 23, 23,
         21, 21, 21, 19, 19, 19, 18, 18, 21, 27, 28, 30, 32, 34, 36, 37, 37, 37, 39, 39, 39, 39, 39, 39,
         41, 41, 41, 41, 41, …
Run Code Online (Sandbox Code Playgroud)

python list

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

如何交换Python列表中的项目?

问题是编写python代码来生成数字{1,2,3,...,n}的所有排列.所以,我写这段代码:

def permute(n):

  if n==len(a):
    print a
    return

  for i in range(n,len(a)):
    swap(i,n)
    permute(n+1)
    swap(i,n)


def swap(x,y):
  a[x],a[y]=a[y],a[x]

a=[1,2,3,4]       #any list 
permute(0)
Run Code Online (Sandbox Code Playgroud)

而且效果很好.但是由于我所拥有的空闲时间,我修改了一下并编写了这段代码:

def permute(n):

  if n==len(a):
    print a
    return

  for i in range(n,len(a)):
    swap(a[i],a[n])           #modification
    permute(n+1)
    swap(a[i],a[n])           #modification


def swap(x,y):
  x,y=y,x                     #modification

a=[1,2,3,4]
permute(0)
Run Code Online (Sandbox Code Playgroud)

这次它没用.但在那之后,我读了一些关于如何在python中将值赋值给不同的内容.

但是我仍然想知道,根据你的说法,第二个代码是错的,这样我就可以交叉检查并讨论我认为出了什么问题!那是我的第一个问题.

我的第二个问题是如何在python列表中进行值的交换?它与简单值会发生什么不同?因为上面的代码似乎都适用.但我无法弄明白这一点我可以让自己理解,而且它进一步让我感到困惑,那就是python如何操纵它的列表.

我确信在Python语言的设计中有一些我不知道导致所有这些混淆的东西.帮助我解决它们,如果可能的话,使用一些图片可视化.那么我很容易理解发生了什么!

python python-2.7

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