0

I have a template that contains one search button (this is for filtering the details based on option choose in the select) and a Next button (this is to move to next tab).

When the template loads for then i will search for projects from the select list and the result will be displayed in a table - which is working for me.

After the result is displayed in the table, i will select a row (through radio button) from that table and click on Next button to move to next tab. Here when i click the Next button i want to update some values in the django database but not able to achieve this. Can some one help me?

My View:

def form(request):
    projects = CreateProjects.objects.filter(Status=True)
    if request.method == 'POST':
        selectproject = request.POST.get('selectproject')
        searchprojlist = ListProjDetails.objects.filter(Project=selectproject)
        return render(request,'form.html',{'projects':projects,'lists': searchprojlist})
    elif request.POST.get('tab1btn','') == 'nxttab':
        selval = ListProjDetails.objects.get(id=1)
        selval.Selected = True
        selval.LockedUser = request.user
        selval.save()
    else:
        lists = ListProjDetails.objects.all()
        return render(request,'form.html',{'projects':projects})

First If POST is working correctly, trouble is with the 2nd IF (2nd button). I have swapped the 2nd IF to be called fresh after the else statement but not working either.

I have passed id=1 for testing purpose only

enter image description here

I have added the image for better understanding, basically when the template loads select project from the list and Search and then choose one from the output and press next that moves to next tab by updating values like select to True and Locked User to current user.

My view as explained before.

My Javascript:

$(function(){
    $('#nexttab').click(function(e){
        e.preventDefault();
        if(document.getElementById('selectradio').checked){
              $('#tabs a[href="#tab2"]').tab('show');
        }
    });
})

Template:

<form method="POST">
                            {% csrf_token %}
                            <div class="row ml-auto">
                                <select class="custom-select mb-4 ml-2" name="selectproject" style="width: 30em;">
                                    <option selected>Choose...</option>
                                    {% for projects in projects %}
                                    <option>{{ projects.ProjName}}</option>
                                    {% endfor %}
                                </select>
                                <div class="ml-4">
                                    <button type="submit" class="btn btn-primary" value="Search">Search</button>
                                </div>
                            </div>
                            
                            <table class="table">
                                <thead class="thead-light">
                                    <tr>
                                        <th scope="col" class="text-center">S.No.</th>
                                        <th scope="col" class="text-center">Project Name</th>
                                        <th scope="col" class="text-center">Doc</th>
                                        <th scope="col" class="text-center">Target Date</th>
                                        <th scope="col" class="text-center">Docnum</th>
                                        <th scope="col" class="text-center">Type</th>
                                        <th scope="col" class="text-center">Select</th>
                                        <th scope="col" class="text-center">Locked by User</th>
                                        <th scope="col" class="text-center">Status</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for lists in lists %}
                                    <tr>
                                        <th scope="row" class="text-center">{{ lists.id }}</th>
                                        <td class="text-center">{{ lists.Project }}</td>
                                        <td class="text-center">{{ lists.doc }}</td>
                                        <td class="text-center">{{ lists.TargetDate }}</td>
                                        <td class="text-center">{{ lists.docnum }}</td>
                                        <td class="text-center">{{ lists.type }}</td>
                                        <td class="text-center"><input class="form-check-input" type="radio"
                                                name="select1" id="selectradio" value="option1"></td>
                                        <td class="text-center">{{ lists.LockedUser }}</td>
                                        <td class="text-center">
                                            {% if lists.DStatus == "Available" %}
                                            <label class="badge badge-success">
                                                {% else %}
                                                <label class="badge badge-warning">
                                                    {% endif %}
                                                    {{ lists.DStatus }}</td>
                                    </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                            <div class="text-right mr-5">
                                <button type="submit" class="btn btn-primary" id="nexttab" name="tab1btn"
                                    value="nxttab">Next</button>
                            </div>

                        </form>
2
  • the code will not enter the elif block because the first if is True. Put your conditions to save the data in the if block itself.
    – Ejaz
    Jul 18, 2020 at 11:05
  • Thanks for your comment :) I understand your suggestion but i want that save to happen when that Next button is clicked. How can i achieve that if i move the save code to first IF please? Jul 18, 2020 at 14:08

1 Answer 1

0

I am just trying to re-factor your code, you should be able to pickup then.

from django.shortcuts import render, redirect
from django.urls import reverse

def form(request):
    projects = CreateProjects.objects.filter(Status=True)
    if request.method == 'POST':
        if request.POST.get('tab1btn','') == 'nxttab':
            selval = ListProjDetails.objects.get(id=1)
            selval.Selected = True
            selval.LockedUser = request.user
            selval.save()  
            return redirect(reverse('name-of-the-view'))  # redirect to form.html   

        selectproject = request.POST.get('selectproject')
        searchprojlist = ListProjDetails.objects.filter(Project=selectproject)
        return render(request,'form.html',{'projects':projects,'lists': searchprojlist})
    else:
        lists = ListProjDetails.objects.all()
        return render(request,'form.html',{'projects':projects})
1
  • Hi Ejaz, this is ain't working... i have added image explaining what i am doing... any help would be great!! I think the issue is with the condition i have set at Javascript for moving to next tab, but i dont know how to resolve that Jul 19, 2020 at 9:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.