小编xpa*_*nta的帖子

将模板变量渲染为HTML

我使用'messages'接口将消息传递给用户,如下所示:

request.user.message_set.create(message=message)
Run Code Online (Sandbox Code Playgroud)

我想在我的{{ message }}变量中包含html 并在不转义模板中的标记的情况下呈现它.

django django-templates

171
推荐指数
6
解决办法
14万
查看次数

Django:TemplateSyntaxError:无法解析余数

我打字时遇到这个问题localhost:8000/admin/.

`TemplateSyntaxError:无法解析余数:来自'admin:password_change'的':password_change'.在Django 1.5中更改了'url'的语法,请参阅文档.

这是我的一部分settings.py:

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'grappelli',
    'filebrowser',
    # Uncomment the next line to enable the admin:
     'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    #'django.contrib.admindocs',
     'tinymce',
     'sorl.thumbnail',
     'south',
     'django_facebook',
     'djcelery',
     'devserver',
     'main',
)
AUTH_USER_MODEL = 'django_facebook.FacebookCustomUser'

AUTHENTICATION_BACKENDS = (
    'django_facebook.auth_backends.FacebookBackend', 
    'django.contrib.auth.backends.ModelBackend',
    # Uncomment the following to make Django tests pass:
    'django.contrib.auth.backends.ModelBackend',
)
Run Code Online (Sandbox Code Playgroud)

我做错了什么吗?

PS:这是我的全部追溯 https://gist.github.com/anonymous/e8c1359d384df7a6b405

编辑:

我按照要求粘贴grep的输出:

$ ack-grep --type=python -r ':password_change' .
lib/python2.7/site-packages/django/contrib/admin/sites.py
264:url = …
Run Code Online (Sandbox Code Playgroud)

django django-admin django-facebook

38
推荐指数
3
解决办法
11万
查看次数

如何使用Python将'NULL'值插入PostgreSQL数据库?

NULL当变量None在Python中时,是否有一个很好的做法可以将键值输入PostgreSQL数据库?

运行此查询:

mycursor.execute('INSERT INTO products (user_id, city_id, product_id, quantity, price) VALUES (%i, %i, %i, %i, %f)' %(user_id, city_id, product_id, quantity, price))
Run Code Online (Sandbox Code Playgroud)

结果在AA TypeError异常,当user_idNone.

如何使用驱动程序NULL将值插入到数据库中?Nonepsycopg2

python postgresql null psycopg2 nonetype

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

django有条件地过滤物体

我想使用一组过滤器从我的数据库中检索一堆行.

我想知道条件过滤器是否适用于django.也就是说,"如果变量不是None则过滤,否则不应用过滤".

像这样的东西:

user = User.objects.get(pk=1)
category = Category.objects.get(pk=1)
todays_items = Item.objects.filter(user=user, date=now()).conditional_filter(category=category))
Run Code Online (Sandbox Code Playgroud)

我想要做的是仅在类别不是None时应用类别过滤器.

如果category为None(表示未在请求对象中给出),则根本不应用此过滤器.这样可以省去一堆'if-elif-else'的情况.

有没有办法做到这一点?

django

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

Django:获取切片后无法更新查询

我想这样做:

UserLog.objects.filter(user=user).filter(action='message').filter(timestamp__lt=now)[0:5].update(read=True)
Run Code Online (Sandbox Code Playgroud)

但我收到这个错误:

Cannot update a query once a slice has been taken.
Run Code Online (Sandbox Code Playgroud)

(使用django 1.2.1)

我究竟做错了什么?

django django-queryset

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

使用ugettext_lazy时,django"不是JSON可序列化的"?

我有这个 views.py

response_dict = {
    'status': status,
    'message': message
}
return HttpResponse(simplejson.dumps(response_dict),
                    mimetype='application/javascript')
Run Code Online (Sandbox Code Playgroud)

自从我开始使用这个导入:

from django.utils.translation import ugettext_lazy as _

在这一行:

message = _('This is a test message')

我收到此错误:

 File "/home/chris/work/project/prokject/main/views.py", line 830, in fooFunc
    return HttpResponse(simplejson.dumps(response_dict),

  File "/usr/local/lib/python2.7/json/__init__.py", line 243, in dumps
    return _default_encoder.encode(obj)

  File "/usr/local/lib/python2.7/json/encoder.py", line 207, in encode
    chunks = self.iterencode(o, _one_shot=True)

  File "/usr/local/lib/python2.7/json/encoder.py", line 270, in iterencode
    return _iterencode(o, 0)

  File "/usr/local/lib/python2.7/json/encoder.py", line 184, in default
    raise TypeError(repr(o) + " is not JSON serializable")

TypeError: <django.utils.functional.__proxy__ …
Run Code Online (Sandbox Code Playgroud)

python django json

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

django:正确检索日期和时间比现在大的数据

我有一个描述这样一个事件的模型:

class Event(models.Model):
    date = models.DateField()
    time = models.TimeField()
Run Code Online (Sandbox Code Playgroud)

我想检索所有未来事件(即日期大于now.date()).但是如果今天是今天,我想要检索今天的事件,时间大于now.time().

这就是我在做的事情:

events = Event.objects.filter(date__gte=now.date()).filter(time__gte=now.time()).order_by('-date')
Run Code Online (Sandbox Code Playgroud)

哪里 now = datetime.datetime.now()

但这是错误的,因为如果唯一的事件是明天而且它的时间小于当前时间(例如明天的事件是09:00,今天是19:00),它会给我一个空集.

这可能在django吗?

PS:我想避免迭代整个集合.

django django-models

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

django 1.4如何从客户端自动获取用户的时区

我想知道是否有办法从客户端自动检索用户的时区.特别是在登录时.

我试图在登录页面中添加它(使用auth.login):

{% get_current_timezone as TIME_ZONE %}
Run Code Online (Sandbox Code Playgroud)

然后在登录表单中添加它

<input type="hidden" name="next" value="/redirect/?tz={{ TIME_ZONE }}">
Run Code Online (Sandbox Code Playgroud)

tz始终是服务器的时区.

python django timezone

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

Django,将所有未经过身份验证的用户重定向到登录页面

我有一个django网站,里面有很多网址和观点.现在,我已要求将所有未经过身份验证的用户重定向到某个目标网页.因此,所有视图都必须检查是否user.is_authenticated()返回到一组新的着陆页.

它可以以一种漂亮的方式完成,而不是弄乱我views.py/ urls.py那么多吗?

python django

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

蟒蛇.我可以检查SMTP服务器是否已断开连接(我可以再次连接吗?)

我正在使用smtplib,我正在从我的应用程序发送通知电子邮件.但是我有时会注意到(特别是当邮件发送之间有很多空闲时间时)我收到SMTPServerDisconnected错误.

我想这有两个解决方案(不知道它们都没有)

  1. 增加发送邮件之间的空闲时间
  2. 连接断开时重新连接.

我认为第二种解决方案似乎更优雅.但是我该怎么做呢?

编辑:我正在添加代码

from smtplib import SMTP
smtp = SMTP()
smtp.connect('smtp.server.com')
smtp.login('username','password')

def notifyUser():
    smtp.sendmail(from_email, to_email, msg.as_string())
Run Code Online (Sandbox Code Playgroud)

python smtplib

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