0
對於清點界面,我希望用戶爲庫存中的新數量項插入小數。具有這個小數點的交易是通過增加差值來設置從股票獲取到實際值的項目。如何使用自定義表單對對象執行操作來創建基於類的視圖
要做到喲我已經加入了插入小數
views.py只提供一個字段的視圖:
class StorageItemStockTakingView(FormView):
form_class = StockTakingForm
success_url = reverse_lazy('storage_item_list')
template_name = 'pmgmt/storageitem/stocktaking.html'
def form_valid(self, form):
si = StorageItem.objects.get(pk=self.kwargs["pk"])
si.stock_report(
Decimal(self.request.POST["amount"]), self.request.user)
return super(StorageItemStockTakingView, self).form_valid(form)
基於自定義表單中forms.py
class StockTakingForm(forms.Form):
amount = forms.DecimalField(
_("Parts now inside storage"),
max_digits=10,
decimal_places=4,
help_text=_("The amount of currently inside storage place."))
所用的模板是安靜簡單:
{% extends "base.html" %}
{% load i18n %}
{% load semanticui %}
{% block content %}
<h1>{{ object }}</h1>
<p>{% trans "It will create an Transaction adjusting the amount for items on stock to reported one.." %}</p>
<form method="post" class="ui form segment">
{% csrf_token %}
{{ form|semanticui }}
<input class="ui button blue" type="submit" value="Submit">
</form>
{% endblock %}
這確實與Python2一起工作。但我使用這個在Python3時,我得到一個錯誤
2016-12-27 19:17:57,206 django.request 24760 Thread-2 ERROR Internal Server Error: /pmgmt/storageitem/1/stocktaking/
Traceback (most recent call last):
File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/core/handlers/base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/core/handlers/base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/contrib/auth/decorators.py", line 23, in _wrapped_view
return view_func(request, *args, **kwargs)
File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/views/generic/base.py", line 88, in dispatch
return handler(request, *args, **kwargs)
File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/views/generic/edit.py", line 221, in post
if form.is_valid():
File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/forms/forms.py", line 161, in is_valid
return self.is_bound and not self.errors
File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/forms/forms.py", line 153, in errors
self.full_clean()
File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/forms/forms.py", line 362, in full_clean
self._clean_fields()
File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/forms/forms.py", line 380, in _clean_fields
value = field.clean(value)
File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/forms/fields.py", line 168, in clean
self.run_validators(value)
File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/forms/fields.py", line 151, in run_validators
v(value)
File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/core/validators.py", line 310, in __call__
if self.compare(cleaned, self.limit_value):
File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/core/validators.py", line 324, in <lambda>
compare = lambda self, a, b: a > b
File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/utils/functional.py", line 148, in __lt__
return self.__cast() < other
TypeError: unorderable types: str() < decimal.Decimal()
2016-12-27 19:17:57,206 django.request 24760 Thread-2 ERROR Internal Server Error: /pmgmt/storageitem/1/stocktaking/
Traceback (most recent call last):
File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/core/handlers/base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/core/handlers/base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/contrib/auth/decorators.py", line 23, in _wrapped_view
return view_func(request, *args, **kwargs)
File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/views/generic/base.py", line 88, in dispatch
return handler(request, *args, **kwargs)
File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/views/generic/edit.py", line 221, in post
if form.is_valid():
File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/forms/forms.py", line 161, in is_valid
return self.is_bound and not self.errors
File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/forms/forms.py", line 153, in errors
self.full_clean()
File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/forms/forms.py", line 362, in full_clean
self._clean_fields()
File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/forms/forms.py", line 380, in _clean_fields
value = field.clean(value)
File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/forms/fields.py", line 168, in clean
self.run_validators(value)
File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/forms/fields.py", line 151, in run_validators
v(value)
File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/core/validators.py", line 310, in __call__
if self.compare(cleaned, self.limit_value):
File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/core/validators.py", line 324, in <lambda>
compare = lambda self, a, b: a > b
File "/home/frlan/quellen/partuniverse/p3/lib/python3.5/site-packages/django/utils/functional.py", line 148, in __lt__
return self.__cast() < other
TypeError: unorderable types: str() < decimal.Decimal()
所以我想知道什麼是基於類的視圖上下文做這樣的事情最近的Django版本的正確方法是什麼?
這不是你的錯誤,這是我看到現在已經確定的來源,但我個人會用' form.cleaned_data ['amount']'作爲arg到'si.stock_report'而不是'Decimal(self.request.POST [「amount」])''。 –