K.Pardo
K.Pardo

Reputation: 131

Django - Form data won't save to the database

I created a "vanilla" form that when submitted should check to see if an Item exists based on the text input. If it does will increment a counter on that items "item_selected" field then continue to create a requisition record using that item and the rest of the submitted fields. Else the item does not exist and it will create the item record as well as creating the requisition record. When submit my form nothing is happening. I made sure the url routing was correct and is being submitted to the correct view.

I dont believe I can use a ModelForm because ModelForms bind to one model and here I am trying to update multiple tables. Thank you in advance!

Views.py

def create_req(request):
    if request == 'POST':
        req_form = ReqForm(request.POST)
        if req_form.is_valid():
            try:
                item_record = ItemMaster.objects.get(item=req_form.cleaned_data['item'])
                item_record.save(commit=False)
                item_record.item_selected = F('item_selected') + 1
                item_record.save()

                requisition_record = Requisition.objects.create(item=req_form.cleaned_data['item'], 
                                                                signature=req_form.cleaned_data['signiture'])
                requisition_record.save(commit=False)
                requisition_record.username = CustomUser.objects.get(username=request.user)
                requisition_record.save()
            except ItemMaster.DoesNotExit:
                item_record = ItemMaster.objects.create(item=req_form.cleaned_data['item'], 
                                                        description=req_form.cleaned_data['description'], 
                                                        price=req_form.cleaned_data['price'], 
                                                        item_selected=1)
                item_record.save()

                requisition_record = Requisition.objects.create(item=req_form.cleaned_data['item'], signature=req_form.cleaned_data['signiture'])
                requisition_record.save(commit=False)
                requisition_record.username = request.user
                requisition_record.save()
            return HttpResponseRedirect(reverse('home') )
    else:
        req_form = ReqForm()
    return render(request, 'req/create_req.html', {'req_form':req_form})

forms.py

class ReqForm(forms.Form):
    item = forms.CharField(min_length=3, max_length=20)
    description = forms.CharField(min_length=0, max_length=50, empty_value='' )
    price = forms.DecimalField(max_digits=19, decimal_places=2)
    signiture = forms.CharField(min_length=0, max_length=10, empty_value='')

create_req.html

<p>
    <a href="{% url 'home' %}">Home</a> &nbsp
    <a href="{% url 'requisition:create_req' %}">Reset Form</a>
</p>

<h1>Example Form</h1>
{% if error_message %}
<p>
    <strong>{{error_message}}</strong>
</p>
{% endif %}

<body>  
    <form method="post" action="{% url 'requisition:create_req' %}" >
      {% csrf_token %}
      {{ req_form }}
      <input type="submit" value="Submit">
    </form>
</body>

Urls.py

app_name = 'requisition'
urlpatterns = [
    path('', views.create_req, name='create_req'),
]

Upvotes: 0

Views: 607

Answers (1)

samschultz
samschultz

Reputation: 359

change

if request == "POST":
    ...

to

if request.method == "POST":
    ...

Upvotes: 1

Related Questions