重定向到Django中的另一个页面

时间:2020-05-11 00:55:50

标签: html django

我正在Django中的一个项目中工作,有人试图填充某些患者的信息,然后单击“提交”按钮后,我想将其重定向到包含所有现有患者列表的页面中,我正在尝试使用html中的动作标签,但似乎不起作用,我想知道我在做什么错。

html

{%extends 'base.html'%}
{%load staticfiles%}
{%block body_block%}
<link rel="stylesheet" href="{%static 'patients/css/patientform.css'%}">
    <form action="{% url 'patients'%}"  method="POST">
        <div class="wrapper">
        {%csrf_token%}
        <div class="Patient">
            <h3>Informacion del Paciente</h3>
            {{patientinfo.as_p}}
        </div>
        <div class="Medical">
            <h3>Informacion Medica</h3>
            {{medicalinfo.as_p}}
        </div>
        <div class="Insurance">
            <h3>Informacion de Seguro</h3>
            {{insuranceinfo.as_p}}
        </div>

        <div class="FirstRelative">
            <h3>Antecedentes Familiares</h3>
            <h5>Primer Caso</h5>
            {{first_relative.as_p}}
            <h5>Segundo Caso</h5>
            {{second_relative.as_p}}
        </div>
        </div>
        <input id="submit" type="submit" value="Agregar">
    </form>
{%endblock%}

网址模式

from django.urls import path
from .views import *

urlpatterns = [
    path('',PatientsList.as_view(),name='patients'),
    path('addpatient',PatientFormView,name='addpatient'),
]

2 个答案:

答案 0 :(得分:1)

应该在 views.py

中检索发布请求后进行重定向
    # AT POST REQUEST END   
    return redirect("patients")

Django文档: https://docs.djangoproject.com/en/3.0/topics/http/shortcuts/#redirect

答案 1 :(得分:0)

PatientFormView的最后,您应使用以下内容进行重定向:

   return redirect("patients")

有关更多详细信息,请查看Django文档:docs.djangoproject.com/en/3.0/topics/http/shortcuts/#redirect