Django Modal提交后关闭

时间:2020-08-23 07:56:53

标签: django bootstrap-modal

我已经开发了一种简单的表单,可以将其加载到引导程序模式中,可以呈现该表单,并且一切正常。但是,当我提交表单时,模态不提交并关闭,因为我得到重定向错误是不正确的。我正在使用没有Jquery的Bootstrap Modal来简化实现,表单是通过Detail View函数加载的,而提交是通过另一个View函数'test1'进行的。

我遇到以下错误:

/ split / 3处的NoReverseMatch 反向查找“ applicationdetail”,未找到任何参数。尝试了1个模式:['applicationdetail /(?P [0-9] +)$']

下面是我的概念代码:

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>
```````````````````

1 个答案:

答案 0 :(得分:1)

您忘记在pk方法中指定redirect自变量:


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)