我有一个奇怪的问题.我可以从命令行使用psql连接到远程主机,但不能在django设置文件中使用完全相同的凭据.我收到错误:
无法连接到服务器:权限被拒绝服务器是否在主机"remote ip"上运行并接受端口port_number上的TCP/IP连接?
如果我可以使用psql连接而不使用django我在这里缺少什么?我还检查了python解释器,我可以加载psycopg2.
任何帮助都非常感谢,因为我在网上找到的并没有帮助.
干杯,
d
这是settings.py中的db conf
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': '<db name goes here>', # Or path to database file if using sqlite3.
'USER': '<db user>', # Not used with sqlite3.
'PASSWORD': '<pswd goes here>', # Not used with sqlite3.
'HOST': '<remote ip goes here>',
'PORT': '5432',
}
Run Code Online (Sandbox Code Playgroud) 我需要获得给定集合的所有可能组合.例如,给定:[1,4,7],得到的组合应该是:
我尝试使用next_permutation方法,但它不是我想要的(这不会返回像111,144,717等的值).
那么我有什么方法可以用C++做到这一点?请注意,我是一个完整的初学者.
提前致谢.
我有一个多索引数据框:
C
A B
25 58 16.0
59 135.0
60 36.0
Run Code Online (Sandbox Code Playgroud)
我想转换为dicts/objects列表:
[
{A: 25, B: 58, C: 16},
{A: 25, B: 59, C: 135},
{A: 25, B: 60, C: 36}
]
Run Code Online (Sandbox Code Playgroud)
我能够重置索引df.reset_index:
A B C
0 25 58 16.0
1 25 59 135.0
2 25 60 36.0
Run Code Online (Sandbox Code Playgroud)
并df.to_dict('index')用来得到这个:
[
{0: {A: 25, B: 58, C: 16}},
{1: {A: 25, B: 59, C: 135}},
{2: {A: 25, B: 60, C: 36}}
]
Run Code Online (Sandbox Code Playgroud)
这是接近的,但我不想在最终的dict中包含索引.
有没有一种简单的方法来实现这一目标?
如何在模板中创建自定义模板标记以使用关键字args?
custom_templates.py
from django import template
register = template.Library()
@register.simple_tag
def custom_tag_field(value, kwarg1=False, kwarg2=False):
t = template.loader.get_template('some_template.html')
return t.render(template.Context({'value': value, 'kwarg1':kwarg1, 'kwarg2': kwarg2}))
Run Code Online (Sandbox Code Playgroud)
template.py
{% load custom_templates %}
Run Code Online (Sandbox Code Playgroud)
....
我想使用带有关键字args的自定义模板标记,如下所示:
{% custom_tag_field form.somefield "value" kwarg1="somearg1" kwarg2="somearg2" %)
Run Code Online (Sandbox Code Playgroud) 我有一个测试 django 应用程序。
在一页中,测试向所有用户显示相同的问题。
我希望当用户正确回答时,向其他活动用户的浏览器发送信号以刷新下一个问题。
我一直在学习 django 中的信号,我正在学习使用它们,但我现在不知道如何将“刷新信号”发送到客户端浏览器。
我认为它可以使用 javascript 代码来检查某个值(实际问题)是否更改,如果更改会重新加载页面,但我不知道这种语言,而且我发现的信息很混乱。
有谁能够帮助我?
非常感谢。
我有两个列表,我试图在python中与<或>进行比较.
一个(list1)是斜率然后是水平线(图片是大约130度的钝角),第二个是具有负斜率的线性函数,我从第一个列表(list2)计算.我想这样做是比较两个列表list1,并list2创建一个新的list3地方list3IN =所有的点list1> list2.我无法确定如何处理此问题.我尝试了列表理解,但是我得到了错误
ValueError:具有多个元素的数组的真值是不明确的.
v = [c for c in f if c > y]
list1= [0.0, 0.36, 0.34, 0.32, 0.32, 0.3, 0.3, 0.28, 0.28, 0.26, 0.26, 0.24, 0.24, 0.22, 0.22, 0.2, 0.2, 0.18, 0.18, 0.16, 0.16, 0.14, 0.14, 0.12, 0.12, 0.1, 0.1, 0.08, 0.08, 0.06, 0.06, 0.04, 0.04, 0.02, 0.02, 0.0,..., 0.0]
list2= [ 0.36 0.35 0.34 ..., -9.62 -9.63 -9.64]
Run Code Online (Sandbox Code Playgroud)
任何帮助,建议或指导将不胜感激.如果我的问题不清楚,也请告诉我.
我有一个包含 ContentType 字段的模型。
在任何模型方法中,我都可以将其与字符串进行比较:
self.content_type == "construction" # True if ContentObject points to Construction model.
Run Code Online (Sandbox Code Playgroud)
但是,这样的事情似乎在模板中不起作用。
我尝试的第一件事
{% if object.content_type == "construction" %}
Run Code Online (Sandbox Code Playgroud)
第二:
def __unicode__(self):
return str(self.content_type)
`{% if object == "construction" %}`
Run Code Online (Sandbox Code Playgroud)
它是错误的,但是 {{ object }} 打印construction.
我试图设置一个'help'的全局变量来显示我在代码中的函数.目标是用户在"帮助"一词中输入的任何时候我都可以显示我创建的表格.该函数称为table(),我想显示它.我的代码如下:
def table():
header=(" Operation Command-line Format")
underline=("_________ ___________________")
meat=("Union set1&set2 \n Intersection set1|set2 \n Difference set1-set2 \n Symmetric difference set1 ^ set2 \n Cartesian product set1 X set2 \n Subset set1<=set2 \n Proper subset set1<set2 \n Super set set1=>set2 \n Proper super set set1>set2 \n Cardinality card set1 \n Membership-in xE set1 where x is single element\n Membership-not-in x!Eset1 where x is single element\n Power set power set1\n Display this table help")
print(header, "\n",underline,"\n",meat)
def intro():
firstChoice=input("Would you like …Run Code Online (Sandbox Code Playgroud) 我正在读取具有UTF8编码的CSV文件:
ifile = open(fname, "r")
for row in csv.reader(ifile):
name = row[0]
print repr(row[0])
Run Code Online (Sandbox Code Playgroud)
这很好用,并打印出我希望它打印出来的东西; UTF8编码str:
> '\xc3\x81lvaro Salazar'
> '\xc3\x89lodie Yung'
...
Run Code Online (Sandbox Code Playgroud)
此外,当我只是打印str(而不是repr())输出显示确定(我不明白 - 这不应该导致错误吗?):
> Álvaro Salazar
> Élodie Yung
Run Code Online (Sandbox Code Playgroud)
但是当我尝试将我的UTF8编码转换strs为unicode:
ifile = open(fname, "r")
for row in csv.reader(ifile):
name = row[0]
print unicode(name, 'utf-8') # or name.decode('utf-8')
Run Code Online (Sandbox Code Playgroud)
我得到了臭名昭着的:
Traceback (most recent call last):
File "scripts/script.py", line 33, in <module>
print unicode(fullname, 'utf-8')
UnicodeEncodeError: 'ascii' codec can't encode …Run Code Online (Sandbox Code Playgroud) 在详细研究之后我放弃了,请帮助修复这个Meta类值.我在尝试使用"get_absolute_url"处理模板URL时遇到错误,因为它响应以下错误.
TypeError:'class Meta'获得无效属性:sale_price,get_absolute_url.
以下是我的代码.
class Meta:
db_table = 'products'
ordering = ['-created_at']
def __unicode__(self):
return self.name
@models.permalink
def get_absolute_url(self):
return ('catalog_product', (), {'product_slug': self.slug})
def sale_price(self):
if self.old_price > self.price:
return self.price
else:
return None
Run Code Online (Sandbox Code Playgroud)
谢谢.
django ×5
python ×5
algorithm ×1
c++ ×1
content-type ×1
csv ×1
encoding ×1
list ×1
page-refresh ×1
pandas ×1
postgresql ×1
python-2.7 ×1
python-3.x ×1
templates ×1
unicode ×1
utf-8 ×1