我正在创建一个库存管理系统,现在我正在尝试使表单输入更加有效。
我有2个表,分别为Arrival
和Withdraw
表Arrival
中有prod_cd
,prod_nm
,...,withdraw
表Withdraw
中有prod_cd
,prod_nm
,withdraw
我想让表单仅输入prod_cd
,然后prod_nm
表中的withdraw
和Withdraw
字段将自动检索
我尝试制作另一个页面,所以首先要进行查询以检索Withdraw.objects
,然后添加记录,但会引发错误
views.py
def add_cycle(request, model, cls, inquiry):
if request.method == "POST":
form = cls(request.POST)
if form.is_valid():
form.save()
return redirect(inquiry)
else:
form = cls()
return render(request, 'add_new.html', {'form': form})
def add_arrival(request):
return add_cycle(request, Arrival, ArrivalForm, inquiry_arrival)
def inquiry_cycle(request, pk, model, cls):
instance= Withdraw.objects.get(pk=pk)
form = cls(instance=instance)
if request.method == "POST":
form = cls(request.POST,instance=instance)
if form.is_valid():
form.save(commit=True)
return redirect ('index')
else:
form = ArrivalForm(instance=instance)
return render_to_response('add_newInquiry.html', {'form': form})
def inquiry_arrival (request, pk):
return inquiry_cycle(request, pk, Arrival, ArrivalForm)
urls.py
url(r'^add_arrival$', add_arrival, name='add_arrival'),
url(r'^inquiry_arrival$', inquiry_arrival, name='inquiry_arrival'),
forms.py
class ArrivalForm(forms.ModelForm):
class Meta:
model = Arrival
fields = ('prod_cd', 'prod_nm', 'quantity', 'issues', 'location', 'withdraw', 'expired_date', 'sup_sheet')
add_new.html
<form method="POST">
<br>
{% csrf_token %}
<h4>ADDING ITEMS</h4>
<div class="form-group row">
<label for="id_{{ form.prod_cd.name }}" class="col-2 col-form-label"> {{ form.prod_cd.label }} </label>
<div class="col-10">
{{ form.prod_cd }}
</div>
</div>
<button type="submit" class="btn btn-primary" name="button"> Inquiry</button>
</form>
add_newInquiry.html
<form method="POST">
<br>
{% csrf_token %}
<h4>ADDING ITEMS</h4>
{% for field in form %}
<div class="form-group row">
<label for="id_{{ field.name }}" class="col-2 col-form-label"> {{ field.label }} </label>
<div class="col-10">
{{ field }}
</div>
</div>
{% endfor %}
<button type="submit" class="btn btn-primary" name="button"> Add Record</button>
</form>
我希望我的表单可以从数据库中检索一些字段值,但是我仍然遇到错误
ValueError at /add_arrival
The view inventory.views.add_arrival didn't return an HttpResponse object. It returned None instead.
答案 0 :(得分:0)
让我们一起追踪错误!因此,The view inventory.views.add_arrival didn't return an HttpResponse object. It returned None instead.
的意思是,当Django尝试获取 add_arrival 视图时,不会返回任何模板。
让我们进一步查看 add_arrival 视图。它调用函数 add_cycle 。注意最后一个参数 inquiry_arrival ,这是一种方法。
现在,在 add_cycle 函数中,如果该表格有效,则我们返回redirect(inquiry)
,其中 inquiry 是 inquiry_arrival 方法。但是,由于 inquiry 是一种方法,因此需要调用它以返回某些内容!要调用此方法,您应该在查询后添加括号,例如:redirect(inquiry())
。有关更多信息,请参阅此link。祝你好运!