0

EDIT: I Have identified the cause as the python packages supplied with RHEL 7. The solution was to install an alternate version. In my case, I just moved the server to centOS, where the supplied python works with django.

I have a form which actions towards my home view, it contains two buttons, save and cancel. When run on the local dev server (manage.py runserver) this works fine. When I pushed this to production, the cancel button returns form validation errors, despite not calling the is_valid method.

Here is the view:

def home(request):
#uses home.html
if request.method == 'POST':
    #Figure out which button was pressed
    #Cancel Button - Back to home
    if request.POST.get("cancel"):
        #return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
        footer = request
        lineitems = Budget.build(request.user)
        c = {'lineitems': lineitems,
             'footer':footer,}
        return render(request, 'home.html', c)
    #Save button on config.html IncomeForm/Expenses Form
    if request.POST.get("config_save"):
        #ExpensesForm submitted
        if 'expenseName' in request.POST:
            form = ExpensesForm(request.POST)
            if form.is_valid():
                form.save()
            else:
                temp = 'config.html'     
                footer = 'Expense Form Invalid'
                c = {'form':form,
                     'footer':footer,}
                return render(request, temp, c)
        #IncomeForm submitted
        else:
            form = IncomeForm(request.POST)
            if form.is_valid():
                form.save()
            else:
                form = IncomeForm(request.POST)
                temp = 'config.html'     
                footer = 'Form Invalid'
                c = {'form':form,
                     'footer':footer,}
                return render(request, temp, c)
        #Use Budget Class to populate a table in template
        Budget.update_data({'months':12,
                            'user':request.user})
        temp = 'home.html'
        footer = '* Line Modified'
        lineitems = Budget.build(request.user)
        c = {'lineitems': lineitems,
             'footer':footer,}
        return render(request, temp, c)
# if a GET (or any other method) we'll load the budget
else:
    footer = '* Line item modified'
    footer = request
    Budget.update_data({'user': request.user,
                        'months':12})
    lineitems = Budget.build(request.user)
    c = {'lineitems': lineitems,
         'footer':footer,}
    return render(request, 'home.html', c)

Here is the template:

{% extends "base.html" %}
{% load bootstrap3 %}

{% block title %}
<h1>Add {{ itemtype }}</h1>
{% endblock %}

{% block content %}
<form action="{% url 'home' %}" method="post">
{% csrf_token %}
{{ form.as_p }}
<div class="btn-group">
<input type="submit" name="config_save" value="Save" class="btn btn-primary"/>
<input type="submit" name="cancel" value="Cancel" class="btn btn-default"/>
</div>
</form>
{% endblock %}
{% block footer %}
{{ footer }}
{% endblock %}

EDIT*

I was able to recreate this issue in the dev environment when I replaced

{{ form.as_p }}

With

{% bootstrap_form form layout='vertical' %}

But unfortunately neither of these worked when run on the apache/wsgi server.

Here are my forms. Note I also tried removing the class:form-control and it made no difference. I have another form and view that behaves almost identical to this (cancel is handled with an else, form is modelform) that works, the only difference being there are no date fields. To rule out that being the issue I excluded the date fields but still had the same issue.

#Edit form to add/edit Expenses and Bills
class ExpensesForm(forms.ModelForm):
    class Meta:
        model = Items
        exclude = ('skiplst',)
        widgets = {'user': forms.HiddenInput(),
                   'itemType': forms.HiddenInput(),
                   'itemName': forms.TextInput(attrs={'class':'form-control',}),
                   'category': forms.Select(attrs={'class':'form-control',}),
                   'itemAmount': forms.NumberInput(attrs={'class':'form-control',}),
                   'payCycle': forms.Select(attrs={'class':'form-control',}),
                   'itemNote': forms.TextInput(attrs={'class':'form-control',}),
                   'nextDueDate': forms.DateInput(attrs={'name': 'date',
                                                         'class':'form-control'}),
                   'endDate': forms.DateInput(attrs={'name': 'date',
                                                     'class':'form-control'})}

#Edit form to add/edit Income Sources
class IncomeForm(forms.ModelForm):
    class Meta:
        model = Items
        exclude = ('category','skiplst')
        widgets = {'user': forms.HiddenInput(),
                   'itemType': forms.HiddenInput(),
                   'itemName': forms.TextInput(attrs={'class':'form-control',}),
                   'itemAmount': forms.NumberInput(attrs={'class':'form-control',}),
                   'payCycle': forms.Select(attrs={'class':'form-control',}),
                   'itemNote': forms.TextInput(attrs={'class':'form-control',}),
                   'nextDueDate': forms.DateInput(attrs={'name': 'date',
                                                         'class':'form-control'}),
                   'endDate': forms.DateInput(attrs={'name': 'date',
                                                     'class':'form-control'})}
Edyoucaterself
  • 167
  • 1
  • 1
  • 9

1 Answers1

0

It looks like (slightly hard to tell as the formatting/indentation is off slightly) when your form is submitting the cancel value, it's submitting Cancel, and you're checking for cancel, so that logic is never executed.

Withnail
  • 3,128
  • 2
  • 30
  • 47
  • I changed the logic in the view slightly, now it looks for config_save, and the rest goes to an else statement that redirects to the home view, still the same results. I can also verify its not executing the code for config_save as if I fill the form out and click cancel it does not add a record to the database. I edited my question just now, I believe it has something to do with bootstrap/formatting of the template. – Edyoucaterself Sep 29 '16 at 13:41