0

I'm making an Inventory Management System, which now I'm trying to make my form input more efficient.

I have 2 tables of Arrival and Withdraw

In table Arrival there are prod_cd , prod_nm, ..., withdraw

In table Withdraw there are prod_cd, prod_nm, withdraw

I want to make my form only input the prod_cd and then the field of prod_nm and withdraw would automatically retrieved from the Withdraw table

I've try to make another page so there will be Inquiry first to retrieve the Withdraw.objects, and then Add the record but it throw an error

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>

I expect my form would retrieve some of it fields value from database, but I still got an error

ValueError at /add_arrival
The view inventory.views.add_arrival didn't return an HttpResponse object. It returned None instead.
Ahtisham
  • 9,170
  • 4
  • 43
  • 57
Adonis
  • 1
  • 1
  • Replace contents of `add_arrival` function with `add_cycle` and `inquiry_arrival` with `inquiry_cycle` – Ahtisham Jan 20 '19 at 04:45
  • @Ahtisham where should I replace it ? views.py ? Because actually I use the add_cycle and inquiry_cycle to be inherited There are some other similar function like add_arrival in my views.py – Adonis Jan 20 '19 at 05:33

1 Answers1

0

Let's trace the error together! So, The view inventory.views.add_arrival didn't return an HttpResponse object. It returned None instead. What this means is that when Django attempts to fetch the add_arrival view, no templates are returned.

Let's take a further look at add_arrival view. It calls the function add_cycle. Pay attention to the last parameter inquiry_arrival, which is a method.

Now, in the add_cycle function, if the form is valid, we return redirect(inquiry) where inquiry is the inquiry_arrival method. However, since inquiry is a method, it needs to be called for something to be returned! To call this method, you should have added brackets behind inquiry, like so: redirect(inquiry()). Refer to this link for further information. Good luck!

Jordan Tan
  • 53
  • 3
  • 9