简体   繁体   中英

Redirecting into another page in Django

I am working in a project in Django where someone tries to fill the info of some patients and after hitting the submit button i would like o redirect it into a page with the list of all the existing patients, i am trying using a action tag in the html but it seems not to work, i would like to know what i am doing wrong.

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%}

Url patterns

from django.urls import path
from .views import *

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

Redirection should be made after Post request retrieval in your views.py

    # AT POST REQUEST END   
    return redirect("patients")

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

In the end of your PatientFormView you should redirect with use of:

   return redirect("patients")

For more details check Django documentation: docs.djangoproject.com/en/3.0/topics/http/shortcuts/#redirect

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM