Mohammad Al Blooshi
Mohammad Al Blooshi

Reputation: 109

Django Modal Close after submit

i have developed a simple form loading into bootstrap modal, the form gets rendered and everything is ok. however when i submit the form the modal does not submit and close as i get the Error of redirect is incorrect. i am using Bootstrap Modal without Jquery for ease of implementation, the form is loaded through the Detail View function and the submit is through another View function 'test1'.

i get the following Error:

NoReverseMatch at /split/3 Reverse for 'applicationdetail' with no arguments not found. 1 pattern(s) tried: ['applicationdetail/(?P[0-9]+)$']

below is my concept code :

models.py:

class Startup ( models.Model ) :
    author = models.OneToOneField ( User , on_delete = models.CASCADE )
    startup_name = models.CharField ( max_length = 32 , null = False , blank = False )

class Score_Appeal(models.Model):
    appeal_score = models.ForeignKey(Startup, on_delete = models.CASCADE)
    appeal_evaluator = models.ForeignKey(User, on_delete = models.CASCADE)
    appeal = models.CharField ('Appeal', max_length = 100 , null = False , blank = False , choices = Choice.EVALUATION , default = '' )
    appeal_comment = models.TextField(max_length = 100, blank = True)

views.py:

@login_required
@inv_required
def global_list(request):
    startup = Startup.objects.all()
    return render(request, 'inv_template/global_list.html', {'startup': startup})

@login_required
@inv_required
def applicationdetail(request, pk):
    obj = Startup.objects.filter(pk = pk)
    form = SplitForm1
    return render(request, 'inv_template/application_detail.html', {'data': obj, 'form':form})

@login_required
@inv_required
def test1 (request, id):
    obj = Startup.objects.filter (id=id)[0]
    R = get_object_or_404 ( Startup , startup_name = obj )

    if request.method == 'POST' :
        form = SplitForm1 (request.POST)
        instance = form.save ( commit = False )
        instance.appeal_score = R
        instance.appeal_evaluator = request.user
        instance.save ( )
        return redirect ( 'applicationdetail' )
```````````````

**urls.py:**

````````````
    path ( 'globallist/' , views.global_list , name = 'globallist' ) ,
    path ( 'applicationdetail/<int:pk>' , views.applicationdetail , name = 'applicationdetail' ) ,
    path ( 'split/<int:id>' , views.test1 , name = 'split' ) ,
```````````
**Templates (the detail view with the modal in it):**

``````````````
 <div class="tab-content flex-wrap" style="background-color: rgba(255,255,255,0);width: 500px;padding-left: 8px;height: 650px;">
                <div role="tabpanel" class="tab-pane fade show active" id="tab-1" style="width: 1200px;">
                    <div class="row" style="width: 1326px;min-width: -5px;">
                            <div class = "col" style="text-align: right; margin-bottom: 8px;">
            <button data-href-template="{%url 'split' data.id %}" class="btn btn-warning" data-toggle="modal" data-target="#exampleModal" type="button" style="background-color: rgb(63,129,177);">Evaluate <i class="fas fa-file-medical-alt"></i></button>
    </div>

<div id="modal-open">
    <div aria-labelledby="exampleModalLabel" class="modal fade" id="exampleModal" role="dialog" tabindex="-1">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title">Modal Title</h4>
                    <button aria-label="Close" class="close" data-dismiss="modal" type="button"><span aria-hidden="true">×</span></button>
                </div>
                <div class="modal-body">
                    <form method="post" action="{% url 'split' data.id %}">
                        {% csrf_token %}
                    {{form}}
                </div>
                <div class="modal-footer">
                    <button class="btn btn-primary" type="submit" style="background-color:rgb(255,255,255);" >Submit</button>
                    <button class="btn btn-warning" data-dismiss="modal" style="background-color:rgb(255,139,160);" type="button">Close</button>
                </div>
                </form>
            </div>
        </div>
    </div>
```````````````````

Upvotes: 0

Views: 873

Answers (1)

neverwalkaloner
neverwalkaloner

Reputation: 47354

You forgot to specify pk argument in redirect method:


def test1 (request, id):
    obj = Startup.objects.filter (id=id)[0]
    R = get_object_or_404 ( Startup , startup_name = obj )

    if request.method == 'POST' :
        form = SplitForm1 (request.POST)
        instance = form.save ( commit = False )
        instance.appeal_score = R
        instance.appeal_evaluator = request.user
        instance.save ( )
        return redirect('applicationdetail', pk=id)

Upvotes: 1

Related Questions