我正在努力解决这个问题,当我正常运行我的应用程序时会话工作但我无法弄清楚如何在我的测试用例中设置会话中的数据.
文档说在测试用例中,您必须保存会话以在发出请求之前应用更改.https://docs.djangoproject.com/en/1.2/topics/testing/#persistent-state
例如
from django.test import TestCase
class TestLogin(TestCase):
def test_processuser(self):
redirect = '/processuser/'
session = self.client.session
session["id"] = '1234'
session.save()
response = self.client.get(redirect)
Run Code Online (Sandbox Code Playgroud)
但是从self.client.session返回的会话对象只是一个普通的python dict?
进入Client.session调用的代码是这样的:
def _session(self):
"""
Obtains the current session variables.
"""
if 'django.contrib.sessions' in settings.INSTALLED_APPS:
engine = import_module(settings.SESSION_ENGINE)
cookie = self.cookies.get(settings.SESSION_COOKIE_NAME, None)
if cookie:
return engine.SessionStore(cookie.value)
return {}
session = property(_session)
Run Code Online (Sandbox Code Playgroud)
cookie = self.cookies.get(settings.SESSION_COOKIE_NAME, None)
返回None
所以它只返回一个dict而不是会话存储.
在我保存会话之前,看起来我必须在测试客户端做更多的准备工作?没有真正的经验,任何帮助将不胜感激.
Django 1.2.5 Python 2.6.5
干杯,
阿西姆.
我已经学习 Python 和 GAE 两天了,提前感谢您的帮助。
我有一个 HTML 输入数组,如下所示:
<input type="text" name="p_item[]">
<input type="text" name="p_item[]">
<input type="text" name="p_item[]">
Run Code Online (Sandbox Code Playgroud)
我想用Python解析输入,我正在尝试这个,但不起作用:
items = self.request.get('p_item')
for n in range(1,len(items)):
self.response.out.write('Item '+n+': '+items[n])
Run Code Online (Sandbox Code Playgroud)
这样做的正确方法是什么?
我在理解如何安装xhtml2pdf时遇到了问题.我按照Chris Glass网站上的说明进行了虚拟环境设置并运行了单元测试.当我在我的views.py
文件顶部插入以下内容时:
import ho.pisa as pisa
import cStringIO as StringIO
import cgi
import os
Run Code Online (Sandbox Code Playgroud)
我得到一个错误说No module named ho.pisa
(我预期).我如何获得django甚至python命令行导入此模块(因为我尝试在命令行中导入模块具有相同的成功级别).
编辑
为什么我需要虚拟环境.我不能只是xhtml2pdf
作为一个包安装并正常导入或者我会破坏一些Django/python的东西吗?
编辑
我还从xhtml2pdf虚拟环境中运行了django,但(xhtml2pdfenv)person@person-laptop:~/Software/django$ ./manage.py runserver
仍拒绝导入xhtm2pdf
在一个if condidition中,何时使用双重否定是有意义的?我发现了这种模式几次:
JS
if(!!children.length) { ...}
Run Code Online (Sandbox Code Playgroud) 安装NPM和NodeJS的正确方法是什么?
方法1.:sudo apt-get install npm
sudo apt-get install npm
Reading package lists... Done
Building dependency tree
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:
The following packages have unmet dependencies:
npm : Depends: nodejs …
Run Code Online (Sandbox Code Playgroud) 在django-tables2中,默认情况下,所有表列都支持排序.这意味着所有列标题都呈现为链接,允许用户调整表数据的顺序.但我不希望列标题呈现为链接,如何做到这一点?
这是文件!
默认情况下,所有表列都支持排序.这意味着所有列标题都呈现为链接,允许用户调整表数据的顺序.
可以基于表或列禁用排序.
Table.Meta.orderable = False – default to disable ordering on columns
Column(orderable=False) – disable ordering for specific column
Run Code Online (Sandbox Code Playgroud)
例如禁用除一个以外的所有列:
class SimpleTable(tables.Table):
name = tables.Column()
rating = tables.Column(orderable=True)
class Meta:
orderable = False
Run Code Online (Sandbox Code Playgroud)
我这样做,但它不起作用.这是我的talbes.py文件:
class MusicBaseTable(tables.Table):
songs = tables.CheckBoxColumn()
title = tables.Column()
artist = tables.Column()
album = tables.Column()
genre = tables.Column()
date = tables.Column()
class Meta:
orderable = False
attrs = {"class": "list"}
Run Code Online (Sandbox Code Playgroud)
我使用带有密码管理器的开启器,当我第一次使用开启器时,我发出以下警告消息:
/usr/lib/python2.7/urllib2.py:894: UserWarning: Basic Auth Realm was unquoted
url, req, headers)
Run Code Online (Sandbox Code Playgroud)
我的应用似乎工作正常,但我怎么能隐藏这条消息?
PS:我使用:
Python 2.7.3 (default, Jan 2 2013, 16:53:07)
[GCC 4.7.2] on linux2
Run Code Online (Sandbox Code Playgroud) 我有一个dict,看起来像这样:
{
"name1" : { "title":"someTitle", "confidence" : 0.765 },
"name2" : { "title":"someTitle2", "confidence" : 0.9343 },
"name3" : { "title":"someTitle3", "confidence" : 0.0031 },
"name4" : { "title":"someTitle4", "confidence" : 0.23 },
"name5" : { "title":"someTitle5", "confidence" : 0.8768 }
}
Run Code Online (Sandbox Code Playgroud)
在这样的一个字典中,我需要创建另一个类似的字典,只包含具有最高置信度值的值.在这种情况下应该是:name2,name5,name1.
{
"name1" : { "title":"someTitle", "confidence" : 0.765 },
"name2" : { "title":"someTitle2", "confidence" : 0.9343 },
"name5" : { "title":"someTitle5", "confidence" : 0.8768 }
}
Run Code Online (Sandbox Code Playgroud)
应保留Dict结构和内容.
谢谢!
使用Python 2.7我想删除JSON中名称为errCode和errMsg的所有键
我的JSON示例:
json_string = '''\
{
"vacation":
[
{
"dates": [
{
"duration": 360,
"dateTo": "4.2.2014",
"dateFrom": "2.1.2014"
}
],
"worker": "8"
},
{
"dates": [
{
"duration": 420,
"dateTo": "",
"dateFrom": "29.01.2015",
"errCode": "1",
"errMsg": "Missing dateTo"
}
],
"worker": "2"
}
],
"general": {
"scriptComment": "",
"scriptTo": "",
"errCode": "2",
"errMsg": "Missing comment.",
"scriptFrom": "01.01.2014"
}
}
'''
Run Code Online (Sandbox Code Playgroud)
所以我需要删除我的Json中的所有errMsgs和errCodes,而无需考虑该级别
此应用程序将读取邮箱数据 (mbox.txt) 计算每个组织的电子邮件消息数量(即电子邮件地址的域名),使用具有以下架构的数据库来维护计数。
CREATE TABLE Counts (org TEXT, count INTEGER)
Run Code Online (Sandbox Code Playgroud)
当您在 mbox.txt 上运行程序时,上传上面生成的数据库文件以进行评分。如果您在测试中或使用不同的文件多次运行该程序,请确保在每次运行前清空数据。
您可以将此代码用作应用程序的起点:http : //www.pythonlearn.com/code/emaildb.py。此应用程序的数据文件与之前的作业相同:http : //www.pythonlearn.com/code/mbox.txt。
第一次学习Sqlite。我对这项任务感到非常困惑,尽管它似乎很容易。我不知道如何将 Python 代码连接到 Sqlite。似乎他们不需要代码作为赋值。所有需要的是数据库文件。我应该如何解决这个问题。不知道如何开始。非常感谢!