I am creating Django web application for additional payments like Shift allowance on call etc, I am getting HttpResponse error

Viewed 21

I am creating Django web application for additional payments like Shift allowance on call etc, I have created several apps under same, shift if one of the among applications, In this app, user will submit shift details and manager will approve it, here I am getting error when manager try to approve users shift details

ValueError at /shift/6/ The view shift.views.update_status didn't return an HttpResponse object. It returned None instead. Request Method: POST Request URL: http://127.0.0.1:8000/shift/6/ Django Version: 3.2.5 Exception Type: ValueError Exception Value:
The view shift.views.update_status didn't return an HttpResponse object. It returned None instead. Exception Location: C:\Users\vikasdange\Anaconda3\lib\site-packages\django\core\handlers\base.py, line 309, in check_response Python Executable: C:\Users\vikasdange\Anaconda3\python.exe Python Version: 3.9.7 Python Path:
['C:\Users\vikasdange\django\additional_payments', 'C:\Users\vikasdange\Anaconda3\python39.zip', 'C:\Users\vikasdange\Anaconda3\DLLs', 'C:\Users\vikasdange\Anaconda3\lib', 'C:\Users\vikasdange\Anaconda3', 'C:\Users\vikasdange\Anaconda3\lib\site-packages', 'C:\Users\vikasdange\Anaconda3\lib\site-packages\locket-0.2.1-py3.9.egg', 'C:\Users\vikasdange\Anaconda3\lib\site-packages\win32', 'C:\Users\vikasdange\Anaconda3\lib\site-packages\win32\lib', 'C:\Users\vikasdange\Anaconda3\lib\site-packages\Pythonwin'] Server time: Sat, 23 Jul 2022 12:38:22 +0000 project name additional_payments

Project additional_payments urls.py file

urlpatterns = [
path('', include('login.urls')),
path('admin/', admin.site.urls),
path('ot/', include('ot.urls')),
path('oncall/', include('oncall.urls')),
path('shift/', include('shift.urls')),
path('accounts/', include('accounts.urls')),     
]

app shift urls.py file

urlpatterns = [


path("shift", views.shift,name="shift"),
path("<int:id>/", views.update_status,name="update_status"),
path("shift_details/", views.shift_details,name="shift_details"),

]

app shift views.py file

def shift_details(request):
sdetails = Shift.objects.all()  
return render(request, 'shift_details.html', {'details':sdetails})

def update_status(request, id):

if request.method == 'POST':
    ar_status = Shift.objects.get(pk=id)
    fm = shiftforms(request.POST,ar_status)
    if fm.is_valid():
        fm.save()
        return redirect('/')

else:
    ar_status=Shift.objects.get(id=id)
    fm = shiftforms(instance=ar_status)
    return render(request, "update.html", {"Shift":ar_status})

   

update.html file

  {% extends 'base.html' %}

{% block content %}

<h4>Approve Reject Request</h4>
<form action="" method="post">
{% csrf_token %}
{{form.as_p}}

<table border="1">

    <tr>
        <td>Employee ID</td>
        <td><input type="text" name="id" value="{{Shift.id}}" readonly></td>
        
    </tr>

    <tr>
        <td>Approval Status</td>
        <td><input type="text" name="status" value="{{Shift.status}}" ></td>
        
    </tr>
</table>

<input type="submit" value="Update">

<a href="{% url 'shift_details' %}">Back To Home</a>
</form>
{% endblock content %}
1 Answers

There is no else if form is not valid. If form is not valid it should render the template with form errors like (and check if ar_status object exists) :

def update_status(request, id):
    try:
         ar_status = Shift.objects.get(pk=id)
    except ObjectDoesNotExist:
         print('There is no shift object with this id')
         return redirect('/')
    if request.method == 'POST':
       
        fm = shiftforms(request.POST, instance=ar_status)
        if fm.is_valid():
            fm.save()
            return redirect('/')
        else:
            print(fm.errors)
    else:
        ar_status=Shift.objects.get(id=id)
        fm = shiftforms(instance=ar_status)
    return render(request, "update.html", {"Shift":ar_status, "form": fm})

notice that the retun with render is no longer part of else statment.

Related