新手问题:我需要从views.py中的方法接受表单中的参数,但它给了我麻烦.在视图中,我创建了一个包含以下代码段的方法:
def scan_page(request):
myClient = request.user.get_profile().client
form = WirelessScanForm(client = myClient) # pass parameter to the form
Run Code Online (Sandbox Code Playgroud)
在forms.py中我定义了以下形式:
class WirelessScanForm(forms.ModelForm):
time = forms.DateTimeField(label="Schedule Time", widget=AdminSplitDateTime())
def __init__(self,*args,**kwargs):
myClient = kwargs.pop("client") # client is the parameter passed from views.py
super(WirelessScanForm, self).__init__(*args,**kwargs)
prob = forms.ChoiceField(label="Sniffer", choices=[ x.sniffer.plug_ip for x in Sniffer.objects.filter(client = myClient) ])
Run Code Online (Sandbox Code Playgroud)
但django一直给我错误说:( TemplateSyntaxError: Caught NameError while rendering: name 'myClient' is not defined查询中发生此错误)
我担心这里会有一些愚蠢的东西,但我无法弄明白为什么.请帮忙,谢谢.
我正在尝试使用json.dumps()Django 将对象编码为json ,但是当我传入python对象时,它会引发此错误.
TypeError: <OrgInvite: OrgInvite object> is not JSON serializable
Run Code Online (Sandbox Code Playgroud)
我假设即使JSON只能编码某些数据类型,其中一种数据类型就是对象.我在Stack Overflow上读到了另一个问题,解决这个问题的一个好方法是使用.__dict__我试过的方法从对象中创建一个字典,它说我的新字典中的一个键,_state是不可序列化的.我不确定这个_state键是从哪里来的,并且想知道有没有办法将我的对象转换为没有那个额外字段的字典,所以我可以将它编码为JSON?
模型:
class OrgInvite(models.Model):
token = models.CharField(max_length=16, unique=True, null=False)
account_id = models.ForeignKey(Account, on_delete=models.CASCADE, null=False)
org_id = models.ForeignKey(Org, on_delete=models.CASCADE, null=False)
used = models.BooleanField(default=False)
is_admin = models.BooleanField(default=False)
name = models.CharField(max_length=70)
email = models.CharField(max_length=255)
Run Code Online (Sandbox Code Playgroud)
视图:
def get_invite(token):
if not token:
raise Exception("Invitation token is not specified")
invitation = OrgInvite.objects.get(token=token)
if not invitation:
raise Exception("Invitation token is invalid.")
return invitation
def invite_accept_redirect(token):
# """ -Redirects to the accept …Run Code Online (Sandbox Code Playgroud) 我查看了django-celery教程,我认为这将帮助我在不让用户等待的情况下运行后台任务.但是,我在程序中有一个特定的要求,当用户输入日期时,django应该能够进行调度并将执行推迟到以后的时间.我之前使用过at程序,但它提供了很多权限问题.但是当我阅读Celery的文档时,我只能看到Celery支持cron所谓的任务@periodic_task.我确信它也提供了at类似的机制,但我找不到任何文档.任何人都可以向我指出一些资源,或者只是告诉我如何实现这一目标?谢谢.
我有一个涉及生物学领域的问题.现在我有4个非常大的文件(每个有1亿行),但结构相当简单,这些文件的每一行只有2个字段,都代表一种基因.
我的目标是:设计一个可以实现以下目标的高效算法:在这4个文件的内容中找到一个圆圈.圆圈定义为:
field #1 in a line in file 1 == field #1 in a line in file 2 and
field #2 in a line in file 2 == field #1 in a line in file 3 and
field #2 in a line in file 3 == field #1 in a line in file 4 and
field #2 in a line in file 4 == field #2 in a line in file 1
Run Code Online (Sandbox Code Playgroud)
我想不出一个解决这个问题的好方法,所以我现在只写了一个暴力 - 愚蠢的4层嵌套循环.我正在考虑将它们按字母顺序排序,即使这可能有点帮助,但是很明显计算机内存不允许我一次加载所有内容.有人能告诉我一个以时间和空间有效的方式解决这个问题的好方法吗?谢谢!!
我正在使用Bloomberg Java api下载交易数据.我需要有人告诉我是否存在可以返回交易假期列表的功能.我查看了手册但找不到一个.如果没有这样的东西,有没有一种方法可以创造一个?谢谢.
我尝试在django admin的fieldsets中创建一个字段以仅显示某些数据,但根据django文档,只list_display显示了一个能够自定义的示例.我尝试了类似的方法,fieldsets如下所示:
在models.py中:
def ports_with_same_scanner(self):
return PortList.objects.filter(scanner=self.scanner)
ports_with_same_scanner.short_description = 'port_lists'
Run Code Online (Sandbox Code Playgroud)
在admin.py中,这不起作用:
fieldsets = (
('Scan Template', {
'fields': ( ('name', 'scanner', 'ports_with_same_scanner',), 'comment', ('in_use',
'fc_growing', 'nc_growing'), 'nvt_prefs')
}),
)
Run Code Online (Sandbox Code Playgroud)
但是,如果我这样做:
list_display = ('name', 'scanner', 'ports_with_same_scanner', 'comment', 'in_use', 'fc_growing', 'nc_growing', 'nvt_prefs')
Run Code Online (Sandbox Code Playgroud)
该ports_with_same_scanner作品就好了.问题是我不想将显示更改fieldsets为list_display,我想知道如何实现相同的功能.谢谢.
我在阻止所有互联网访问的服务器上创建virtualenv时遇到问题.以前有人成功地做过吗?我搜索但没有显示任何有用的东西.我来回传输数据没有问题,但我不知道需要下载哪些软件包以及我需要安装哪些选项.
如果你对我通过尝试创建它感到好奇,这里是回溯:
netops@netops1 /spare/local/latency $virtualenv -p /usr/bin/python2.6 latency
Running virtualenv with interpreter /usr/bin/python2.6
New python executable in latency/bin/python2.6
Also creating executable in latency/bin/python
Installing setuptools.....................
Complete output from command /spare/local/latency/latency/bin/python2.6 -c "#!python
\"\"\"Bootstra...sys.argv[1:])
" --always-copy -U setuptools:
Downloading http://pypi.python.org/packages/2.6/s/setuptools/setuptools-0.6c11-py2.6.egg
Traceback (most recent call last):
File "<string>", line 279, in <module>
File "<string>", line 211, in main
File "<string>", line 159, in download_setuptools
File "/usr/lib64/python2.6/urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "/usr/lib64/python2.6/urllib2.py", line 391, in open
response = …Run Code Online (Sandbox Code Playgroud) 当我试图调用"new"来创建一个指针并将其推入向量时,我得到了段错误.我在向量中推送元素的代码是:
queue->push_back(new Box(gen_id, Interval(x_mid, x_end), Interval(y_mid-y_halfwidth, y_mid+y_halfwidth)));
Run Code Online (Sandbox Code Playgroud)
基本上Box是一个类,构造函数只需要3个参数generation_id,和2 Intervals.在此"推送"之前和之后,我在向量中打印出内容,然后:
[ -0.30908203125, -0.3087158203125 ] , [ -0.951416015625, -0.9510498046875 ]
[ -0.3087158203125, -0.308349609375 ] , [ -0.951416015625, -0.9510498046875 ]
[ -0.30908203125, -0.3087158203125 ] , [ -0.9510498046875, -0.95068359375 ]
[ -0.3087158203125, -0.308349609375 ] , [ -0.9510498046875, -0.95068359375 ]
Run Code Online (Sandbox Code Playgroud)
后:
[ -0.30908203125, -0.3087158203125 ] , [ -0.951416015625, -0.9510498046875 ]
[ -0.3087158203125, -0.308349609375 ] , [ -0.951416015625, -0.9510498046875 ]
[ 8.9039208750109844342e-243, 6.7903818933216500424e-173 ] , [ -0.9510498046875, -0.95068359375 ]
[ -0.3087158203125, -0.308349609375 ] …Run Code Online (Sandbox Code Playgroud) 我没有那么多使用C,最近我对2d数组初始化问题感到困惑.我需要调试某人的代码并坚持以下(她的原始代码):
const int location_num = 10000;
bool **location_matrix;
if (node_locations)
{
location_matrix = (bool **)malloc(location_num*sizeof(bool *));
if (!location_matrix)
{
cout<<"error 1 allocating location_matrix" << endl;
exit;
}
for (i=0; i<location_num; i++)
{
location_matrix[i] = (bool *) malloc(location_num*sizeof(bool ));
if (!location_matrix[i])
{
cout<<"error 2 allocating location_matrix" << endl;
exit;
}
for (j=0; j<location_num; j++)
location_matrix[i][j] = false;
}
}
Run Code Online (Sandbox Code Playgroud)
我认为是多余的,所以我将其更改为以下内容:
location_matrix[location_num][location_num] = { {false} };
但是,分段错误在运行时发生.我的问题是:上面的代码是如何失败的?如果它看起来正确,动态分配和静态分配之间有什么区别?是因为维度可能不是常数,所以我们需要动态地做这个吗?另外,只是为了好奇,我如何malloc 2d数组存储指针?谢谢.
快速提问。我使用语句运行以下代码try except(因为数据库中可能没有条目)。对于某些条目,我遇到了 except 块,尽管数据库中肯定有一个条目!当使用objects.filter()代替时,objects.get()我没有这个问题 - 它永远不会进入数据库中相同条目的 except 块!
key = "anystringasprimarykey"
username = "anyusername"
try:
entry = MyDatabase.objects.get(ort=key, user=username)
except:
print("oh, exception!")
Run Code Online (Sandbox Code Playgroud)
有人可以告诉我我做错了什么吗?