Ad

Multiple Button In Django: Accept And Reject

I am new to Django. I am working on a project where I want accept and reject button and whenever client click on the respective button that object will go into the accept or reject template. I have no idea how can I do this.

This is my .html file which is displaying all the objects and have a accept and reject button:

<div class="body table-responsive">
                        <form id="form" method="POST" action = "{% url 'admin_team_detail' %}">
                                    {% csrf_token %}
                            <table class="table table-hover">
                                <thead>
                                    <tr>
                                        <th>S No.</th>
                                        <th>COMPANY NAME</th>
                                        <th>TEAM MEMBER</th>
                                        <th>EMAIL</th>
                                        <th>STATUS</th>
                                        <th><center>#</center></th>
                                    </tr>
                                </thead>
                                <tbody>

                                        {%for team in object%}
                                    <tr>

                                        <th scope="row"> {{ forloop.counter }}</th>
                                        <td>{{team.company_name}}</td>
                                        <td>{{team.team_member}}</td>
                                        <td>{{team.email}}</td>
                                        <td>-</td>
                                        <td><center><input type="submit" value="accept" name="accept">
                                            <input type="submit" value="reject" name="reject"></center></td> 
                                    </tr>

                                    {% endfor %}

                                </tbody>
                            </table>
                        </form>

Here is views.py:

def admin_team_detail(request):
obj= Create_Team.objects.all()
print(request.method)
if request.method == 'POST':
    if 'reject' in request.POST :
        Create_Team.status = 'reject'
    else:
        Create_Team.status = 'accept'
        Create_Team.save()
return render(request, "admin/team-details.html", {"object": obj})

This is rendering all the objects from database and displaying on the website.

I know that I have to make two templates for accept and reject but I don't know how it will take the objects that have a accept or reject response.

And I also want that if client click on the button then that response will be saved in the database.

And I also want to know that whether I have to add a field in my model.py for status.

Ad

Answer

First your two buttons should send the desired value to your views.py and one hidden input in order to pass the team id

<input type="submit" value="reject" name="status">
<input type="submit" value="accept" name="status">
<input type="hidden" name="id" value={{ team.id }}>

Next, in your views.py

def admin_team_detail(request):
    if request.method == 'POST':
    # First, you should retrieve the team instance you want to update
        team = Create_Team.objects.get(id=request.POST('id'))

        # Next, you update the status
        if request.POST.get('status'):
            team.status = request.POST.get('status')
            team.save()

Note: this example assumes your Team model has a status field in order to store the reject/accept value.

class Team(models.Model):
    # You existing fields...
    status = models.CharField(max_length=30)
Ad
source: stackoverflow.com
Ad