我试图从syncr创建的数据中获取每张专辑的随机照片.模型(缩写)如下所示:
class Album(models.Model):
title = models.CharField(max_length=200)
photos = models.ManyToManyField('Photo')
class Photo(models.Model):
title = models.CharField(max_length=200)
Run Code Online (Sandbox Code Playgroud)
我尝试了很多不同的方法但没有成功.这是另一个容易吗?
拿2:最终代码:
def gallery(request,template_name ='galleries.html'):
albums = Album.objects.select_related().all()
album_list = []
for album in albums:
album_list.append({'title':album.title, 'id':album.id, 'photo':album.random_photo()})
return render_to_response(template_name, {
"album_list": album_list,
})
Run Code Online (Sandbox Code Playgroud) 我希望能够从一段文字中提取字母的类型和数量,其中字母可以是任何顺序.还有一些其他的解析正在进行中,但这一点让我难过!
input -> result
"abc" -> [['a',1], ['b',1],['c',1]]
"bbbc" -> [['b',3],['c',1]]
"cccaa" -> [['a',2],['c',3]]
Run Code Online (Sandbox Code Playgroud)
我可以使用搜索或扫描并重复每个可能的字母,但有一个干净的方式吗?
这是我得到的:
from pyparsing import *
def handleStuff(string, location, tokens):
return [tokens[0][0], len(tokens[0])]
stype = Word("abc").setParseAction(handleStuff)
section = ZeroOrMore(stype("stype"))
print section.parseString("abc").dump()
print section.parseString("aabcc").dump()
print section.parseString("bbaaa").dump()
Run Code Online (Sandbox Code Playgroud) 我已经看到这个问题Bootstrap tagsinput添加标签的id和值,但解决方案对我不起作用:
我发现的是,为了让输入框识别我输入的标签,我要么必须有data-role = tagsinput或者调用$("input").tagsinput().
例如.这适用于识别没有数据角色的标签:
$('#meeting-tags').tagsinput();
$('#meeting-tags').tagsinput({
allowDuplicates: false,
itemValue: 'id', // this will be used to set id of tag
itemText: 'label' // this will be used to set text of tag
});
Run Code Online (Sandbox Code Playgroud)
但这不是:
$('#meeting-tags').tagsinput({
allowDuplicates: false,
itemValue: 'id', // this will be used to set id of tag
itemText: 'label' // this will be used to set text of tag
});
Run Code Online (Sandbox Code Playgroud)
但是,如果我想通过javascript添加项目,那么只有当我既没有数据角色也没有初始调用时它才会起作用.错误是在未设置itemValue选项时无法添加对象
例如.这有效,但现在它无法识别标签:
$('#meeting-tags').tagsinput({
allowDuplicates: false,
itemValue: 'id', // this will be used to set …Run Code Online (Sandbox Code Playgroud) 这应该很简单,这个正则表达式可以很好地搜索以特定字符开头的单词,但是我无法让它匹配哈希和问号.
这适用于匹配开头的单词:
r = re.compile(r"\b([a])(\w+)\b")
Run Code Online (Sandbox Code Playgroud)
但这些不匹配:试过:
r = re.compile(r"\b([#?])(\w+)\b")
r = re.compile(r"\b([\#\?])(\w+)\b")
r = re.compile( r"([#\?][\w]+)?")
Run Code Online (Sandbox Code Playgroud)
甚至试过只匹配哈希
r = re.compile( r"([#][\w]+)?"
r = re.compile( r"([/#][\w]+)?"
text = "this is one #tag and this is ?another tag"
items = r.findall(text)
Run Code Online (Sandbox Code Playgroud)
期待得到:
[('#', 'tag'), ('?', 'another')]
Run Code Online (Sandbox Code Playgroud) 当我尝试编译时,我得到一个"无需重复"的错误:
search = re.compile(r'([^a-zA-Z0-9])(%s)([^a-zA-Z0-9])' % '+test', re.I)
Run Code Online (Sandbox Code Playgroud)
问题是'+'符号.我该怎么处理?
每次打开页面时,我都希望获得当前活动的项目ID.这将通过查找子域并验证当前登录的用户可以查看它来完成.
一旦我达到我的观点,我希望能够做到
tasks = Task.objects.filter(project = current_project)
Run Code Online (Sandbox Code Playgroud)
WHere current_project(或CURRENT_PROJECT或current_project ???)已经设置好了.
任何人都可以解释我在文档中找到的各种方法的优缺点,并让我走上正轨吗?
这就是我最终做到的方式:
装饰:
def check4project(fn):
current_project = 'fred'
def check(*args, **kw):
kw['project']=current_project
return fn(*args, **kw)
return check
Run Code Online (Sandbox Code Playgroud)
查看示例
@login_required
@check4project
@tweetpost
def index(request, project=0):
print project
Run Code Online (Sandbox Code Playgroud) 我一定错过了设置自定义模板上下文的方法,因为它永远不会被调用.
在设置中:
TEMPLATE_CONTEXT_PROCESSORS = (
"django.core.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django_authopenid.context_processors.authopenid",
"web.context_processors.my_hat",
)
Run Code Online (Sandbox Code Playgroud)
在web/context_processors.py中
from libs.utils import get_hat, get_project, my_hats
print 'heloooo'
def my_hat(request):
"""Insert some additional information into the template context
"""
import pdb
pdb.set_trace()
print 'hiiiiiiii'
return {'hat': get_hat(request),
'project': get_project(request),
}
Run Code Online (Sandbox Code Playgroud)
什么都没有输出,django进程查看和显示模板,而没有遇到这个.我错过了什么!?
感谢Insin,我错过了一些内容:
在view.py中
return render_to_response(template, {
'tasks': tasks,
},
context_instance=RequestContext(request))
Run Code Online (Sandbox Code Playgroud)
在模板中:
My current hat is {{hat}}
Run Code Online (Sandbox Code Playgroud) 是否可以在单独的 svg 中“使用”整个其他 svg?我想使用 d3 生成的地图作为同一页面上的图标。这是我尝试过但不起作用的方法。
<svg id="map">
svg stuff here
</svg>
<svg id="bar">
svg stuff here
<use xlink:href="#map" height="20" width="30" ...>
</svg>
Run Code Online (Sandbox Code Playgroud)
还尝试了克隆方法,但最终在另一个 svg 中包含了整个 svg 并且无法扩展。例如。makeicon("#map", "#icon")
function makeicon(source, destination) {
//https://groups.google.com/forum/?fromgroups=#!topic/d3-js/-EEgqt29wmQ
var src = d3.select(source);
var dest = d3.select(destination);
if (!src.empty() && !dest.empty()) {
var newNode = d3.select(dest.node().insertBefore(src.node().cloneNode(true),
src.node().nextSibling))
.attr("id", "newnode")
.attr("width", null) // remove height and width of original svg
.attr("height", null)
.attr("viewBox", "0 0 20 30"); // try to make it smaller
return newNode;
Run Code Online (Sandbox Code Playgroud) Stacked Vertical工作正常:http://jsfiddle.net/Q28Aj/1/
但是,如果我将条形更改为水平(并将标签更改为另一个轴)格式错误:http://jsfiddle.net/e4Rkd/1/

知道我哪里出错了?
我花了整整两天的时间试图让python-mysql去年彻底安装Lion,最后绝望地放弃了.升级到Mountain Lion之后,我想我会再试一次,但没有成功.可能是我对Lion的所有尝试都搞砸了我的设置,我唯一的希望是干净安装,但万一有人可以救我,这里有症状.
首先,我删除了仍然存在的三个版本:
/Users/phoebebr/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.7-i386.egg-tmp
/Users/phoebebr/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.7-i386.egg-tmp/_mysql.so
/Users/phoebebr/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.7-intel.egg-tmp
/Users/phoebebr/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.7-intel.egg-tmp/_mysql.so
/Users/phoebebr/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.7-x86_64.egg-tmp
/Users/phoebebr/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.7-x86_64.egg-tmp/_mysql.so
Run Code Online (Sandbox Code Playgroud)
然后重新安装.一切似乎都没问题,但有警告,似乎是64位版本:
Phoebe-Brights-iMac:.python-eggs phoebebr$ sudo pip install mysql-python
dyld: DYLD_ environment variables being ignored because main executable (/usr/bin/sudo) is setuid or setgid
Password:
Downloading/unpacking mysql-python
Downloading MySQL-python-1.2.4c1.zip (113kB): 113kB downloaded
Running setup.py egg_info for package mysql-python
Downloading http://pypi.python.org/packages/source/d/distribute/distribute-0.6.28.tar.gz
Extracting in /tmp/tmp7fFOxU
Now working in /tmp/tmp7fFOxU/distribute-0.6.28
Building a Distribute egg in /private/tmp/pip-build/mysql-python
/private/tmp/pip-build/mysql-python/distribute-0.6.28-py2.7.egg
Installing collected packages: mysql-python
Running setup.py install for mysql-python
building '_mysql' extension
clang -fno-strict-aliasing -fno-common -dynamic -g -Os -pipe -fno-common …Run Code Online (Sandbox Code Playgroud)