是否可以通过浏览器中的历史记录返回按钮检测用户是否输入了页面?我最好想用angular.js检测这个动作.
我不想使用角度路由.如果用户提交表单并在成功提交到服务器并重新定向之后,它也应该有效,如果用户使用浏览器的后退按钮返回到表单,也应该可以.
我正在运行Mac OS 10.6.8.并希望除了python 2.6还安装python 2.7并在新的virtualenv中使用python 2.7.我执行了以下步骤:
我下载了python 2.7并安装了它:
http://www.python.org/ftp/python/2.7.3/python-2.7.3-macosx10.6.dmg
Run Code Online (Sandbox Code Playgroud)
然后我运行命令使用python2.7设置一个新的virtualenv:
mkvirtualenv --python=python2.7 mynewenv
Run Code Online (Sandbox Code Playgroud)
我的.bash_profile如下所示:
# needed for virtualenvwrapper
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python
export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv
source /usr/local/bin/virtualenvwrapper.sh
# Setting PATH for Python 2.7
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH
Run Code Online (Sandbox Code Playgroud)
现在,当我打开控制台时,我收到以下错误消息.
ImportError: No module named virtualenvwrapper.hook_loader
virtualenvwrapper.sh: There was a problem running the initialization hooks. If Python could not import the module virtualenvwrapper.hook_loader, check that virtualenv has been installed for VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python and that PATH is set properly. …
Run Code Online (Sandbox Code Playgroud) 我想覆盖自定义对象模型管理器,只返回特定用户创建的对象.管理员用户仍应使用对象模型管理器返回所有对象.
现在我找到了一种可行的方法.他们建议创建自己的中间件,如下所示:
#### myproject/middleware/threadlocals.py
try:
from threading import local
except ImportError:
# Python 2.3 compatibility
from django.utils._threading_local import local
_thread_locals = local()
def get_current_user():
return getattr(_thread_locals, 'user', None)
class ThreadLocals(object):
"""Middleware that gets various objects from the
request object and saves them in thread local storage."""
def process_request(self, request):
_thread_locals.user = getattr(request, 'user', None)
#### end
Run Code Online (Sandbox Code Playgroud)
在自定义管理器中,您可以调用该get_current_user()
方法仅返回特定用户创建的对象.
class UserContactManager(models.Manager):
def get_query_set(self):
return super(UserContactManager, self).get_query_set().filter(creator=get_current_user())
Run Code Online (Sandbox Code Playgroud)
这是一个很好的方法来处理这个用例吗?这会有用吗?或者这就像"使用大锤破解坚果"?;-)
只是使用:
Contact.objects.filter(created_by= user)
Run Code Online (Sandbox Code Playgroud)
在每个视图中看起来都不是很整洁.
经过一段时间的测试后,这种方法表现得相当奇怪,通过这种方法,您可以将全局状态与当前请求混合在一起.
使用下面介绍的方法.它非常简单,无需乱搞中间件. …
我想将MongoEngine用于我的下一个项目.现在我想知道我是否也可以在同一个项目中直接使用PyMongo.只是因为我需要一些非常特殊的东西,不能直接通过mongoengine支持.
有没有怀疑这会起作用,或者我不应该这样做!?
我正在使用熊猫,我想知道使用熊猫在开始日期和结束日期之间的最简单方法是什么?
关于在Python中执行此操作有很多帖子(例如),但我有兴趣直接使用pandas,因为我认为pandas可能很容易处理这个问题.
我正在尝试使用angular.js创建一个POST请求到这个Django视图.
class PostJSON4SlickGrid(View):
"""
REST POST Interface for SlickGrid to update workpackages
"""
def post(self, request, root_id, wp_id, **kwargs):
print "in PostJSON4SlickGrid"
print request.POST
return HttpResponse(status=200)
Run Code Online (Sandbox Code Playgroud)
因此我创建了这个资源.
myModule.factory('gridData', function($resource) {
//define resource class
var root = {{ root.pk }};
return $resource('{% url getJSON4SlickGrid root.pk %}:wpID/', {wpID:'@id'},{
get: {method:'GET', params:{}, isArray:true},
update:{method:'POST'}
});
});
Run Code Online (Sandbox Code Playgroud)
在控制器中调用get方法可以正常工作.该网址被翻译为http://127.0.0.1:8000/pm/rest/tree/1/
.
function gridController($scope, gridData){
gridData.get(function(result) {
console.log(result);
$scope.treeData = result;
//broadcast that asynchronous xhr call finished
$scope.$broadcast('mySignal', {fake: 'Hello!'});
});
}
Run Code Online (Sandbox Code Playgroud)
我正面临执行更新/ POST方法的问题. …
我想知道如何在行的内联编辑后触发reloadGrid.
<script type="text/javascript">
jQuery(document).ready(function(){
var lastcell;
jQuery("#grid").jqGrid({
url:'{{ json_data_handler }}?year={{ get_param_year }}&month={{ get_param_month }}&project_id={{ get_param_project_id }}',
datatype: "json",
mtype: 'GET',
colNames:['hour_record_pk', 'project_pk', 'weekday', 'date', 'sum', 'description'],
colModel:[
{name:'hour_record_pk',index:'hour_record_pk', width:55, sortable:false, editable:true, editoptions:{readonly:true,size:10}},
{name:'project_pk',index:'project_pk', width:55, sortable:false, editable:true, editoptions:{readonly:true,size:10}},
{name:'weekday',index:'weekday', width:300, editable:false, sortable:false},
{name:'date_str',index:'date_str', width:300, editable:true, sortable:false, editoptions:{readonly:true}},
{name:'sum',index:'sum', width:100, editable:true, sortable:true,editrules:{number:true,minValue:0,maxValue:24,required:true}},
{name:'description',index:'description', width:600, editable:true, sortable:false,},
],
jsonReader : {
repeatitems:false
},
rowNum:31,
rowList:[10,20,31],
//pager: jQuery('#gridpager'),
sortname: 'id',
viewrecords: true,
sortorder: "asc",
width: 800,
height: 750,
caption:"Hour Record Overview",
reloadAfterEdit: true, //seems …
Run Code Online (Sandbox Code Playgroud) 我想知道TestCase.assertQuerysetEqual方法是如何工作的.我以不同的方式尝试了它,每个都引导我另一个错误信息.
#create a backup of all records in the tree
tree_record_backup = list(Tree.objects.all())
#do some updates on another table, which should not affect the tree table if everything goes wrong
#check if list of tree records did not changed
tree_record_qs = Tree.objects.all()
#Number1:
self.assertQuerysetEqual(tree_record_qs,[repr(tree_record_backup)])
#Number2:
self.assertQuerysetEqual(tree_record_qs,tree_record_backup)
Run Code Online (Sandbox Code Playgroud)
Number1的错误消息:
First list contains 21 additional elements.
First extra element 1:
node.pk: 2 - node: node2 - pk: 2 - level: 0 - ancestor: 2
Run Code Online (Sandbox Code Playgroud)
第2个错误消息:
AssertionError: Lists differ: ['<Tree: node.pk: 1 - node: …
Run Code Online (Sandbox Code Playgroud) Python(Django)中基于Django查询集创建JSON的最快方法是什么.请注意,在此处提议的模板中解析它不是一个选项.
背景是我创建了一个循环遍历树中所有节点的方法,但在转换大约300个节点时已经非常慢.我想到的第一个(也可能是最糟糕的)想法是以某种方式"手动"创建json.请参阅下面的代码.
#! Solution 1 !!#
def quoteStr(input):
return "\"" + smart_str(smart_unicode(input)) + "\""
def createJSONTreeDump(user, node, root=False, lastChild=False):
q = "\""
#open tag for object
json = str("\n" + indent + "{" +
quoteStr("name") + ": " + quoteStr(node.name) + ",\n" +
quoteStr("id") + ": " + quoteStr(node.pk) + ",\n" +
)
childrenTag = "children"
children = node.get_children()
if children.count() > 0 :
#create children array opening tag
json += str(indent + quoteStr(childrenTag) + ": [")
#for …
Run Code Online (Sandbox Code Playgroud)