我正在运行这个django应用程序(也是一个没有经验的python用户)并希望能够访问模型.
我有以下内容:
import sys, os
sys.path.append('/Users/jx/dev/arc/django/second/')
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
from django.db import models
from testproject.models import Location
loc=Location.objects.get(name='Joes')
print loc.name
Run Code Online (Sandbox Code Playgroud)
但是得到以下错误:
Traceback (most recent call last):
File "zz.py", line 6, in <module>
from testproject.models import Location
ImportError: No module named testproject.models
Run Code Online (Sandbox Code Playgroud)
有没有办法我可以确定它是否正确加载settings.py?或者是否存在明显的语法问题?PYTHONPATH应该反映模型的位置(如果打印os.environ,它目前不会)?
谢谢
我正在将一个zend框架/ php应用程序移植到rails,并且有一组参数用于查询.
例如:
/locations/get-all-locations/lat/xxx.xxx/lng/xxx.xxx/radius/5/query/lemonade/updated_prev_hours/72
Run Code Online (Sandbox Code Playgroud)
但是可以提供这样的变化,例如省略查询或距离参数(即基本上可能需要在不同的集合中 - 另一个问题).似乎所有参数都需要命名.
最好是通过像这样的静态处理来处理这个问题:
match 'locations/get-all-locations/lat/:lat/lng/:lng/radius/:radius/query/:query/updated_prev_hours/:updated_prev_hours => 'locations#get_all_locations'
Run Code Online (Sandbox Code Playgroud)
这就是我需要的所有值,这些值将在params哈希中可用吗?或者是否有更好的策略来处理这样的复杂网址?
谢谢
有这个:
import os, os.path
print len([name for name in os.listdir(os.path.expanduser("~")) if os.path.isfile(name)])
Run Code Online (Sandbox Code Playgroud)
但它总是返回0.如何修改此返回文件的数量?
谢谢
我有一个可以有活动的位置.我想要一个upcoming_events方法,但希望它向下舍入,如果有人在晚上10点看,它将显示今天的事件.我有这个:
def upcoming_events
d=Time.new
d.strftime("%m-%d-%Y")
l=Event.where('location_id=? and start_datetime>?',self.id, d)
end
Run Code Online (Sandbox Code Playgroud)
我得到正确的转换,但在d.strftime但查询是:
SELECT `events`.* FROM `events` WHERE (location_id=301 and start_datetime>'2012-06-20 02:49:23')
Run Code Online (Sandbox Code Playgroud)
任何想法如何让它做'2012-06-20'?
这是一个采用的rails应用程序,没有测试.我想在集成测试,测试omniauth,但我得到一个错误(编辑我也基于此:https://github.com/intridea/omniauth/wiki/Integration-Testing).这反映了我对Rspec缺乏了解.似乎默认情况下请求对象可用.
我的spec/spec_helper.rb中有:
config.include IntegrationSpecHelper, :type => :request
Capybara.default_host = 'http://localhost:3000'
OmniAuth.config.test_mode = true
OmniAuth.config.add_mock(:facebook, {
:uid => '12345'
})
Run Code Online (Sandbox Code Playgroud)
在我的spec/integration/login_spec中:
require 'spec_helper'
describe ServicesController, "OmniAuth" do
before do
puts OmniAuth.config.mock_auth[:facebook]
puts request # comes back blank
request.env["omniauth.auth"] = OmniAuth.config.mock_auth[:facebook]
end
it "sets a session variable to the OmniAuth auth hash" do
request.env["omniauth.auth"][:uid].should == '12345'
end
end
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
{"provider"=>"facebook","uid"=>"12345","user_info"=> {"name"=>"Bob示例"}}
F
失败:
1)ServicesController OmniAuth设置会话变量到OmniAuth AUTH散列故障/错误:request.env [ "omniauth.auth"] = OmniAuth.config.mock_auth [:实] NoMethodError:未定义的方法
env' for nil:NilClass # ./login_spec.rb:8:in在块(2级)' …
我有一些项目可以是红色,黄色,绿色(比这更复杂)的几个属性之一,它们有相应的is_red,is_yellow,is_green布尔值.
我希望有一个api函数来改变我传递的值,例如:
id: 12
type: red
Run Code Online (Sandbox Code Playgroud)
在我的功能中有:
item=Item.find(id)
if item.is_ + type == true
item.is_ + type=false
end
Run Code Online (Sandbox Code Playgroud)
我该怎么做?
thx提前
我经常需要在bash命令中引用一段文本,例如:
git mv _fav-locations-cluster.html.erb partials/_fav_locations_cluster.html.erb
Run Code Online (Sandbox Code Playgroud)
编辑#1
请注意,它来自破折号 - >下划线
我想做一些事情(其中$ 0和tab会被评估或者其他东西):
git mv _fav-locations-cluster.html.erb partials/'$0 and tab'
Run Code Online (Sandbox Code Playgroud)
其中$ 0和tab将引用第一个参数.这在bash或zsh中是否可行?
我是 AFNetworking 的新手,正在调用一个返回 json 的简单登录 api,如下所示:
{"status":"success","data":{"auth_token":"12jt34"}}
Run Code Online (Sandbox Code Playgroud)
我通过以下方式执行此操作,但它返回 __NSCFData 而不是我可以操作的东西。
NSURL *baseURL = [NSURL URLWithString:@"http://localhost:3000/arc/v1/api/"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:baseURL];
[httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
[httpClient defaultValueForHeader:@"Accept"];
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
uname,@"email", pwd, @"password",
nil];
[httpClient postPath:@"login-mobile" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *className = NSStringFromClass([responseObject class]);
NSLog(@"val: %@",className);
}failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error retrieving data: %@", error);
}];
Run Code Online (Sandbox Code Playgroud)
它输出:
2013-03-21 14:52:51.290 FbTabbed[21505:11303] val: __NSCFData
Run Code Online (Sandbox Code Playgroud)
但我希望它成为一本我可以操作的字典,我认为它应该如何工作?我究竟做错了什么?
我正在使用capistrano并收到此错误消息:
Please install the pg adapter: `gem install activerecord-pg-adapter` (cannot load such file -- active_record/connection_adapters/pg_adapter)
Run Code Online (Sandbox Code Playgroud)
我从我的Gemfile中删除了宝石'pg' bundle install.我在Gemfile.lock中没有看到pg gem.我还将生产数据库更改为sqlite3适配器.为什么Rails 3.2/Capistrano/Bundler要求这个?我没有使用它,而不是在Gemfile.lock中,我该如何解决它?我正在服务器上使用ubuntu 12.04.
thx提前
此外,当我运行它告诉我运行的东西时,我得到:
deploy@oahu:~$ gem install activerecord-pg-adapter
ERROR: Could not find a valid gem 'activerecord-pg-adapter' (>= 0) in any repository
^CERROR: Interrupted
deploy@oahu:~$ \
Run Code Online (Sandbox Code Playgroud)
**编辑1**
从应用程序的根源
Fri Mar 22$ grep -ri 'pg-adapter' *
Fri Mar 22$
Run Code Online (Sandbox Code Playgroud)
database.yml的
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
# Warning: The database defined as "test" will be erased and
# re-generated …Run Code Online (Sandbox Code Playgroud) python ×2
ruby ×2
afnetworking ×1
bash ×1
bundler ×1
capistrano ×1
django ×1
ios ×1
javascript ×1
rspec ×1
rvm ×1
unix ×1
zsh ×1