我正在尝试运行一个填充脚本,我将它放在tango_with_django教程(https://github.com/leifos/tango_with_django/blob/master/tango_with_django_project/populate_rango.py)中,但是我得到了下面的回溯并且它似乎与Django 1.7中的变化有关?如果有人能解释我在这里做错了什么,我会很感激.
(test_env) C:\Users\WriteCode\test_env\epl>python populate_clubs.py
Traceback (most recent call last):
File "populate_clubs.py", line 4, in <module>
django.setup()
File "c:\Python27\lib\site-packages\django\__init__.py", line 20, in setup
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
File "c:\Python27\lib\site-packages\django\conf\__init__.py", line 46, in __ge
tattr__
self._setup(name)
File "c:\Python27\lib\site-packages\django\conf\__init__.py", line 40, in _set
up
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting LOGGING_CONFIG, b
ut settings are not configured. You must either define the environment variable
DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
(test_env) C:\Users\WriteCode\test_env\epl>
Run Code Online (Sandbox Code Playgroud)
我的populate_clubs.py脚本
import os
import sys
import django
django.setup()
def …Run Code Online (Sandbox Code Playgroud) pandas read_excel 文档说指定sheet_name = None应该返回“所有工作表作为DataFrames的字典”。然而,当我尝试像这样使用它时,我得到了一本字符串字典。
target_file = "C:\file_here.xlsx"
data = pd.read_excel(target_file)
print(type(data))
data = pd.read_excel(target_file, sheet_name = None)
print(type(data))
#print(data)
for sheet in data:
print(type(sheet))
Run Code Online (Sandbox Code Playgroud)
这将返回:
<class 'pandas.core.frame.DataFrame'>
<class 'collections.OrderedDict'>
<class 'str'>
Run Code Online (Sandbox Code Playgroud)
我不明白为什么后者返回字符串。我希望能够将每个工作表作为数据框访问,在该工作表(数据框)上执行字符串替换,然后使用 to_excel 将这些工作表放入新的 xlsx 文件中。但我很困惑为什么for Sheet in data:返回字符串。
如果我在下面的代码片段中打印数据(有序字典),它会将数据帧打印到控制台。所以看起来我没有正确访问字典,但我想确切地了解我做错了什么:
data = pd.read_excel(target_file, sheet_name = None)
print(type(data))
print(data)
Run Code Online (Sandbox Code Playgroud) 我在我的表单中覆盖了 init 方法,现在返回一个错误'TransactionForm' object has no attribute '_errors'。
我希望这能工作,因为我在我的init 中包含了 super ,但是也许我不明白如何正确使用它。一个解释将不胜感激。我需要做什么才能使 form.errors 正常工作?
完整追溯
追溯:
文件“C:\Program Files\Python36\lib\site-packages\django\core\handlers\exception.py”在内部 35. response = get_response(request)
文件“C:\Program Files\Python36\lib\site-packages\django\core\handlers\base.py” in _get_response 128. response = self.process_exception_by_middleware(e, request)
_get_response 126 中的文件“C:\
文件“C:\py\portfolio-project\myportfolio\views.py” in add_transaction 136. return render(request, 'myportfolio/add_transaction.html', {'form': form})
文件 "C:\Program Files\Python36\lib\site-packages\django\shortcuts.py" in render 36. content = loader.render_to_string(template_name, context, request, using=using)
文件 "C:\Program Files\Python36\lib\site-packages\django\template\loader.py" in render_to_string 62. return template.render(context, request)
渲染 61 中的文件“C:\Program Files\Python36\lib\site-packages\django\template\backends\django.py”。返回 self.template.render(context)
渲染 175 中的文件“C:\Program Files\Python36\lib\site-packages\django\template\base.py”。返回 self._render(context)
_render 167 …
当用户使用 jquery 在“BuySell”下拉选项中选择“Sell”时,我希望我的硬币字段的查询集发生变化。下拉列表更改后,我使用 AJAX 发送 Get 请求,在我的视图中选择该请求,然后重新加载表单,这是我在 TransactionForm 的 init 方法中覆盖默认硬币字段查询集的地方。
这没有按预期工作,没有任何改变硬币下拉选项的事情,我没有收到任何错误(包括在检查元素时的网络选项卡中)。
我想知道这是否与我在这里调用表单的方式有关:
form = TransactionForm(user = request.user, coin_price = GetCoin("Bitcoin").price)
Run Code Online (Sandbox Code Playgroud)
和表单 init 方法:
def __init__(self, coin_price = None, user = None, *args, **kwargs):
super(TransactionForm, self).__init__(*args, **kwargs)
if user:
self.user = user
qs_coin = Portfolio.objects.filter(user = self.user).values('coin').distinct()
print("qs_coin test: {}".format(qs_coin))
self.fields['coin'].queryset = qs_coin
Run Code Online (Sandbox Code Playgroud)
完整代码:
形式
class TransactionForm(forms.ModelForm):
CHOICES = (('Buy', 'Buy'), ('Sell', 'Sell'),)
coin = forms.ModelChoiceField(queryset = Coin.objects.all())
buysell = forms.ChoiceField(choices = CHOICES)
field_order = ['buysell', 'coin', 'amount', 'trade_price']
class …Run Code Online (Sandbox Code Playgroud) 我的问题是count函数在这个switch语句中不起作用,我不知道为什么.我知道可能有更好的方法来做到这一点,但我只是想特别想弄清楚这一点.
switch(count($matches[1]))
{
case count($matches[1]) = 1:
print "$matches[1][0] <input type=\"text\" name=\"$matches[1][0]\" /><br />";
break;
case count($matches[1]) = 2:
print "$matches[1][0] <input type=\"text\" name=\"$matches[1][0]\" /><br />
$matches[1][1] <input type=\"text\" name=\"$matches[1][1]\" /><br />";
break;
case count($matches[1]) = 3:
print "$matches[1][0] <input type=\"text\" name=\"$matches[1][0]\" /><br />
$matches[1][1] <input type=\"text\" name=\"$matches[1][1]\" /><br />
$matches[1][2] <input type=\"text\" name=\"$matches[1][2]\" /><br />";
break;
default:
print "Error";
}
Run Code Online (Sandbox Code Playgroud)
我知道count函数和$匹配工作.以下是我现在有两个页面供参考.第一页:
<form action="emailform.php" method="post">
<textarea rows="20" cols="20" name="template[]"></textarea>
<input type="submit" name="submit" value="Store your template here" />
</form>
<p>This is …Run Code Online (Sandbox Code Playgroud) 我的金额列属性设置为max_digits = 13,decimal_places = 7,因为从技术上讲,您可以拥有 10000.0000001 比特币之类的东西。
当我尝试在表单上输入并提交 0.1 个比特币时,出现错误:
确保小数点前不超过 3 位数字。
这没有按预期工作:0.1 不超过 3 位数字,即使是这样,我仍然应该能够设置超过 3 位数字。这里发生了什么?
模型.py
class Transactions(models.Model):
user = models.ForeignKey(User, on_delete = models.CASCADE)
coin = models.CharField(max_length = 64)
buysell = models.CharField(default = 'buy', max_length = 4)
amount = models.DecimalField(max_digits = 13, decimal_places = 7)
trade_price = models.DecimalField(max_digits = 5, decimal_places = 2)
trade_date = models.DateTimeField(auto_now = True)
Run Code Online (Sandbox Code Playgroud)
表格.py
class TransactionForm(forms.ModelForm):
CHOICES = ((1, 'Buy'), (2, 'Sell'),)
coin = forms.ModelChoiceField(queryset = Coin.objects.all())
buysell = …Run Code Online (Sandbox Code Playgroud)