Home > Enterprise >  I cannot send a variable to template in django
I cannot send a variable to template in django

Time:09-14

I am trying to send data to a template but it doesn't get rendered. Here is my code:

views.py

def exam_table(request):
    context = {'patient': 'abcde'}
    return render(request, 'examtable.html', context)

examtable.html

<div >
   <div >
   <h3 >
   <i ></i>$
   <p>{{patient}}</p></h3>
   </div>

urls.py

urlpatterns = [

    # The home page
    path('', views.index, name='home'),
    path('insert_patient',views.insert_patient,name='insert_patient'),
    path('nurse_patient',views.nurse_patient,name='nurse_patient'),
    path('examtable',views.exam_table,name='examtable'),     


    # Matches any html file
    re_path(r'^.*\.*', views.pages, name='pages'),
    

]

The html file loads but the field {{patient}} is blank and doesn't show 'abcde'.

CodePudding user response:

it should pass a dictionary value

def exam_table(request):
    context = {'patient': 'abcde'}
    return render(request, 'yourpage.html', {'context': context})

CodePudding user response:

=== Remove bellow regex and hit URL again it should be work ====

# Matches any html file
    re_path(r'^.*\.*', views.pages, name='pages'),
  • Related