这是堆栈溢出的常见错误,但我无法与他们的错误和我的错误相类似。
在数据库的id列中应该有一个数字(由django自动生成),但是却放了一个查询字典。
- (这些是模式细节,以便我可以发布此帖子)*
型号:
class Price(models.Model):
HourlyPrice = MoneyField(default_currency="GBP",max_digits=19,decimal_places=3,null=True,blank=False) # price per hour
DailyPrice = MoneyField(default_currency="GBP",max_digits=19,decimal_places=2,null=True,blank=True)# price per day
WeeklyPrice = MoneyField(default_currency="GBP",max_digits=19,decimal_places=2,null=True,blank=True)# price per week
MonthlyPrice = MoneyField(default_currency="GBP",max_digits=19,decimal_places=2,null=True,blank=True)# price per month
浏览次数:
def test(request):
if request.method == 'POST':
form = Price(request.POST)
form.save()
return render(request,'app/index.html',
{
'title':'Home Page',
'year':datetime.now().year,
}
)
else:
form = TestPrice(request.GET)
context = {
'form': form,
}
return render(request,'app/test.html',context)
模板:
{% extends "app/layout.html" %}
{% block content %}
<form method="post" action="">
{% csrf_token %}
<table>
{{form}}
</table>
<button type="submit">Submit</button>
</form>
{% endblock %}
堆栈跟踪:
File "C:\Users\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\db\models\fields\__init__.py", line 2018, in get_prep_value
return int(value)
The above exception (int() argument must be a string, a bytes-like object or a real number, not 'QueryDict') was the direct cause of the following exception:
File "C:\Users\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\core\handlers\exception.py", line 56, in inner
response = get_response(request)
File "C:\Users\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\core\handlers\base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\OneDrive\c_CodingFiles\SqlLite Learning\testing datetime\test\test\app\views.py", line 52, in test
form.save()
File "C:\Users\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\db\models\base.py", line 812, in save
self.save_base(
File "C:\Users\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\db\models\base.py", line 863, in save_base
updated = self._save_table(
File "C:\Users\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\db\models\base.py", line 976, in _save_table
updated = self._do_update(
File "C:\Users\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\db\models\base.py", line 1019, in _do_update
filtered = base_qs.filter(pk=pk_val)
File "C:\Users\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\db\models\query.py", line 1421, in filter
return self._filter_or_exclude(False, args, kwargs)
File "C:\Users\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\db\models\query.py", line 1439, in _filter_or_exclude
clone._filter_or_exclude_inplace(negate, args, kwargs)
File "C:\Users\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\db\models\query.py", line 1446, in _filter_or_exclude_inplace
self._query.add_q(Q(*args, **kwargs))
File "C:\Users\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\db\models\sql\query.py", line 1532, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "C:\Users\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\db\models\sql\query.py", line 1562, in _add_q
child_clause, needed_inner = self.build_filter(
File "C:\Users\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\db\models\sql\query.py", line 1478, in build_filter
condition = self.build_lookup(lookups, col, value)
File "C:\Users\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\db\models\sql\query.py", line 1303, in build_lookup
lookup = lookup_class(lhs, rhs)
File "C:\Users\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\db\models\lookups.py", line 27, in __init__
self.rhs = self.get_prep_lookup()
File "C:\Users\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\db\models\lookups.py", line 341, in get_prep_lookup
return super().get_prep_lookup()
File "C:\Users\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\db\models\lookups.py", line 85, in get_prep_lookup
return self.lhs.output_field.get_prep_value(self.rhs)
File "C:\Users\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\django\db\models\fields\__init__.py", line 2020, in get_prep_value
raise e.__class__(
谢谢你的时间
2条答案
按热度按时间k2fxgqgv1#
可能的主要问题之一是,您使用
Price
作为表单,但Price
是一个模型,因此直接将request.POST
(或request.GET
)传递给它没有意义。定义
ModelForm
:然后在视图中使用该
PriceForm
验证数据并将数据保存到数据库中:在GET请求的情况下使用
request.GET
是没有意义的:在这种情况下,我们执行对该页面的第一个请求,因此我们应该呈现一个空表单,而不是一个由(可能)空数据限制的表单,然后显示错误。注意:通常Django模型中字段的名称都是用 snake_case 而不是 PascalCase 写的,所以应该是:
hourly_price
而不是HourlyPrice
。注意:如果POST请求成功,需要创建**
redirect
**[Django-doc]来实现Post/Redirect/Get pattern [wiki]。这样可以避免在用户刷新浏览器时发出相同的POST请求。nwlls2ji2#
要修复这个错误,需要在测试视图中将form = Price(request.POST)替换为form = TestPrice(request.POST)。下面是正确的代码: