对不起,如果这非常明显,但我不知道为什么这不起作用.
我正在尝试创建一个jsFiddle.但是,我似乎无法将任何javscript附加到任何元素.例如,onclick不会调用我的js函数.
它没有比这个小提琴更简单.我从W3编辑器中复制粘贴它工作得很好 - 点击按钮,获得警报!
<button onclick="myFunction()">Click me</button>
function myFunction() {alert("sss");}
Run Code Online (Sandbox Code Playgroud)
http://jsfiddle.net/tpip/NYkQN/2/
我在这里失踪了什么?
如何将参数传递给表单?
someView()..
form = StylesForm(data_dict) # I also want to pass in site_id here.
class StylesForm(forms.Form):
# I want access to site_id here
Run Code Online (Sandbox Code Playgroud)
编辑,基于以下答案:
someView()..
form = StylesForm(data_dict, site_id = 1)
class StylesForm(forms.Form):
def __init__(self,*args,**kwargs):
self.site_id = kwargs.pop('site_id')
super(StylesForm,self).__init__(*args,**kwargs)
height = forms.CharField(widget=forms.TextInput(attrs={'size':site_id}))
Run Code Online (Sandbox Code Playgroud) 我有这个:
dates = soup.findAll("div", {"id" : "date"})
Run Code Online (Sandbox Code Playgroud)
不过,我需要的id是一个通配符搜索,因为id可以date_1,date_2等等.
如何为Django响应添加响应头?我有:
response = HttpResponse()
response['Cache-Control'] = 'no-cache'
return render(request, "template.html", {})
# Alternately using render_to_response
# return render_to_response("template.html", {})
Run Code Online (Sandbox Code Playgroud) 从PhantomJS,我如何写入日志而不是控制台?
在示例https://github.com/ariya/phantomjs/wiki/Examples中,它总是(在我看过的那些)中说出如下内容:
console.log('some stuff I wrote');
Run Code Online (Sandbox Code Playgroud)
这不是那么有用.
我正在使用Python的热门分析器:http://docs.python.org/2/library/hotshot.html
它显示了如何打印统计数据:
stats.print_stats(20)
Run Code Online (Sandbox Code Playgroud)
但是我怎么把它变成文件呢?我不知道如何获取信息,因此我可以使用write()将其写入文件.
编辑:
我喜欢与以这种方式打印时相同的易读性结果:
stats = hotshot.stats.load("stones.prof")
stats.strip_dirs()
stats.sort_stats('time', 'calls')
stats.print_stats(20)
Run Code Online (Sandbox Code Playgroud)
所以它看起来像这样:
ncalls tottime percall cumtime percall filename:lineno(function)
1 3.295 3.295 10.090 10.090 pystone.py:79(Proc0)
Run Code Online (Sandbox Code Playgroud)
(所以当我打开stones.prof时它看起来不像)
我想检查名为weekday的输入并且值为1.我尝试了以下这一行.它检查所有工作日.
$('input[name = weekday], [value =1]').attr("checked", "checked");
Run Code Online (Sandbox Code Playgroud) 有没有办法检查Django模板中的空查询集?在下面的示例中,如果有注释,我只希望显示NOTES标题.
如果我在"for"中放置一个{%empty%},那么它会显示空标记内的任何内容,因此它知道它是空的.
我希望有一些不涉及运行查询的东西.
{% if notes - want something here that works %}
NOTES:
{% for note in notes %}
{{note.text}}
{% endfor %}
{% endif %}
Run Code Online (Sandbox Code Playgroud)
澄清:上面的示例"如果注释"不起作用 - 即使使用空查询集,它仍然显示标题.
这是视图的简化版本
sql = "select * from app_notes, app_trips where"
notes = trip_notes.objects.raw(sql,(user_id,))
return render_to_response(template, {"notes":notes},context_instance=RequestContext(request))
Run Code Online (Sandbox Code Playgroud)
编辑:视图选择从多个表中选择.
我有以下(简化)代码,它使用以下来源:
<html>
<p>line 1</p>
<div>
<a>line 2</a>
</div>
</html>
soup = BeautifulSoup('<html><p>line 1</p><div><a>line 2</a></div></html>')
ele = soup.find('p').nextSibling
somehow_print_tag_of_ele_here
Run Code Online (Sandbox Code Playgroud)
我想得到ele的标签,在这种情况下是"div".但是,我似乎只能得到它的孩子的标签.我错过了一些简单的事吗?我以为我可以做ele.tag.name,但这是一个例外,因为tag是None.
#Below correctly prints the div element "<div><a>line 2</a></div>"
print ele
#Below prints "None". Printing tag.name is an exception since tag is None
print ele.tag
#Below prints "a", the child of ele
allTags = ele.findAll(True)
for e in allTags:
print e.name
Run Code Online (Sandbox Code Playgroud)
在这一点上,我正在考虑做一些事情来获得ele的父母,然后得到父母的孩子的标签,并计算了多少上层兄弟姐妹,倒数到正确的子标签.这看起来很荒谬.
如何将内存zipfile写入文件?
# Create in memory zip and add files
zf = zipfile.ZipFile(StringIO.StringIO(), mode='w',compression=zipfile.ZIP_DEFLATED)
zf.writestr('file1.txt', "hi")
zf.writestr('file2.txt', "hi")
# Need to write it out
f = file("C:/path/my_zip.zip", "w")
f.write(zf) # what to do here? Also tried f.write(zf.read())
f.close()
zf.close()
Run Code Online (Sandbox Code Playgroud) django ×3
python ×3
javascript ×2
django-forms ×1
file-io ×1
httpresponse ×1
jquery ×1
jsfiddle ×1
phantomjs ×1
profiling ×1
stringio ×1
tags ×1
zip ×1