重定向不一致

时间:2019-05-11 23:17:16

标签: django redirect

我希望收到一条消息,该消息基于我单击的按钮的类型。取而代之的是,当我单击一个按钮并向我发送另一本不应显示的页面时,django的反应很奇怪。 Urls.py

re_path(r'^liste-ue/$', views.liste_ue, name="liste_ue"),
re_path(r'^ajouter-ue/$', views.ajouter_ue, name="ajouter_ue"),
re_path(r'^modifier-ue/(?P<code>[a-zA-Z0-9 .-]{4,10})/$', views.modifier_ue, name="modifier_ue"),

Views.py

# La view pour qui permet de lister et de créer des UEs. Seul les administrateurs y ont accèes.
def liste_ue(request):
    ues = UE.objects.all();
    return render(request, 'felyn/admin/liste_ue.html', {'ues': ues});


# La view qui permet d'ajouter les UEs. seul les administrateurs y ont accès.
def ajouter_ue(request):

    if request.method == 'POST':

        ue_form     = UEForms(request.POST);
        if ue_form.is_valid():          
            code_ue     = ue_form.clean_code_ue();
            intitule_ue = ue_form.clean_intitule_ue();
            semestre    = ue_form.cleaned_data['semestre'];

            if 'continuer' in request.POST:
                ue_form.save();
                ue_form = UEForms();    
                return render(request, 'felyn/admin/ajouter_ue.html', {'ue_form':ue_form});     
            elif 'quitter' in request.POST:
                ue = ue_form.save();
                ues = UE.objects.all();
                return render(request, 'felyn/admin/liste_ue.html', {'ues': ues});
        else:
            return render(request, 'felyn/admin/ajouter_ue.html', {'ue_form': ue_form});

    else:
        ue_form = UEForms();
        return render(request, 'felyn/admin/ajouter_ue.html', {'ue_form': ue_form});


# La view qui permet de supprimer une UE
def modifier_ue(request, code):

    if request.method == 'POST':

        ue_form = UEForms(request.POST);
        if ue_form.is_valid() and 'supprimer' in request.POST:
            return HttpResponse('supprimer');

        elif ue_form.is_valid() and 'continuer' in request.POST:
            return HttpResponse('continuer');

        elif ue_form.is_valid() and 'quitter' in request.POST:
            return HttpResponse('quitter');

        else:   
            return render(request, 'felyn/admin/ajouter_ue.html', {'ue_form': ue_form});

    else:
        ue  = UE.objects.get(code_ue=code);
        ue_form = UEForms(instance=ue);
        return render(request, 'felyn/admin/modifier_ue.html', {'ue_form': ue_form, 'ue': ue, 'code': code});

ajouter_ue.html

    <form action="{% url 'ajouter_ue' %}" method="post"> 
        {% csrf_token %}
        {{ ue_form.non_field_errors }}
        <div class="form-group row">
            <label for="{{ ue_form.id_for_label }}" class="col-sm-2 col-form-label">Code</label>

            <div class="col-sm-5"> {{ ue_form.code_ue.errors }}{{ ue_form.code_ue }}</div>
        </div>
        <div class="form-group row">
            <label for="{{ ue_form.id_for_label }}" class="col-sm-2 col-form-label">Intitulé</label>
            <div class="col-sm-5">{{ ue_form.intitule_ue.errors }}{{ ue_form.intitule_ue }}</div>
        </div>

        <div class=" form-group row">
            <label for="{{ ue_form.id_for_label }}" class="col-sm-2 col-form-label">Semestre</label>
            <div class="col-sm-5">{{ ue_form.semestre.errors }}{{ ue_form.semestre }}</div>
        </div>
        <div class="form-group">
            <button type="submit" class="btn btn-primary" name="continuer">Enregistrer et ajouter à nouveau</button>
            <button type="submit" class="btn btn-primary" name="quitter">Enregistrer</button>
        </div>
    </form>

modifier_ue.html


<form action="{% url 'modifier_ue' code=code %}" method="post"> 
        {% csrf_token %}
        {{ ue_form.non_field_errors }}
        <div class="form-group row">
            <label for="{{ ue_form.id_for_label }}" class="col-sm-2 col-form-label">Code</label>

            <div class="col-sm-5"> {{ ue_form.code_ue.errors }}{{ ue_form.code_ue }}</div>
        </div>
        <div class="form-group row">
            <label for="{{ ue_form.id_for_label }}" class="col-sm-2 col-form-label">Intitulé</label>
            <div class="col-sm-5">{{ ue_form.intitule_ue.errors }}{{ ue_form.intitule_ue }}</div>
        </div>

        <div class=" form-group row">
            <label for="{{ ue_form.id_for_label }}" class="col-sm-2 col-form-label">Semestre</label>
            <div class="col-sm-5">{{ ue_form.semestre.errors }}{{ ue_form.semestre }}</div>
        </div>
        <div class="form-group">
                <button type="submit" class="btn btn-danger" name="supprimer">Supprimer</button>
            <button type="submit" class="btn btn-primary" name="continuer">Enregistrer et ajouter à nouveau</button>
            <button type="submit" class="btn btn-primary" name="quitter">Enregistrer</button>
        </div>
    </form>

当我在模板“ modifier_ue.html”中并单击一个按钮时,URL告诉我我在模板“ modifer_ue.html”中,但页面的呈现方式是“ ajouter_ue.html” 。因此,URL是正确的,但不是正确的呈现。该如何解决?

在avair做了几次测试之后,我意识到了ue_form()。 is_valid将永远是错误的。造成这部分代码

>>>ue   = UE.objects.get(code_ue='INF401')
>>>ue_form = UEForms(instance=ue)
>>>ue_form.is_valid()
False

如何使实例作为参数传递,使表格始终有效?

0 个答案:

没有答案