0

I am trying to send data to a template but it doesn't get rendered. The html file loads but the field {{patient}} is blank and doesn't show 'abcde'.

Here is my code:

views.py

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

examtable.html

<div class="row d-flex align-items-center">
   <div class="col-9">
   <h3 class="f-w-300 d-flex align-items-center m-b-0">
   <i class="feather icon-arrow-up text-c-green f-30 m-r-10"></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'),
]
Ivan Starostin
  • 8,798
  • 5
  • 21
  • 39
cmoris
  • 1
  • 1
  • 1
    Welcome to Stack Overflow. You can go through the [Formatting help](https://stackoverflow.com/help/formatting) page in the help center to learn how to format your posts. Are you sure you are using the correct url (Probably should be `http://localhost/examtable`)? Your code should work properly... – Abdul Aziz Barkat Sep 14 '22 at 08:20
  • My mistake . I use 'return render(request, '..\\templates\home\\examtable.html', context)' whick works fine in other def's in views but here nothing – cmoris Sep 14 '22 at 08:26
  • 1
    Use "view page source" in your browser to see if the problem is Django or something in your HTML. There's a suspicious dollar in there `$` – nigel222 Sep 14 '22 at 08:41
  • A `` may also be missing in your HTML, unless you copy-pasted a bit too fast. – scūriolus Sep 14 '22 at 08:46
  • I didn't paste the whole html because it's way too big and it's for now static.. The $ sign is just plain text wcich is shown in the html – cmoris Sep 14 '22 at 08:55
  • I think the problem might be in the urls or the settings because I had made it work exactly this way in another template( I mean with my views like this) but now I got stuck – cmoris Sep 14 '22 at 09:32
  • @cmoris try to paste it in place that you are sure exists (like `My title {{ patient }}`). This part of code might be hidden or something. Technically everything seems ok. – NixonSparrow Sep 14 '22 at 11:58
  • I tried to do it with the template of index.html which works fine and does exactly what i want but neither with this template i can achieve it.. – cmoris Sep 14 '22 at 12:18
  • Note, `

    ` is not valid inside `

    `. You can validate your markup here https://validator.w3.org/#validate_by_input

    – Ivan Starostin Sep 14 '22 at 19:40
  • the problem is not with the html.. it doesn't send data from views.py to the html template – cmoris Sep 15 '22 at 08:47

1 Answers1

0

it should pass a dictionary value

def exam_table(request):
    context = {'patient': 'abcde'}
    return render(request, 'yourpage.html', {'context': context})
Starwolf
  • 108
  • 8
  • That's not true. Then it would have to be used like `{% for key, value in context.items %}` to get values. – NixonSparrow Sep 14 '22 at 09:08
  • es but, context is the disctionary. You are passing the context not patient. Try {{context.patient}} – Starwolf Sep 14 '22 at 09:53
  • You don't get value by key from dictionary with a simple dot... https://stackoverflow.com/questions/8000022/django-template-how-to-look-up-a-dictionary-value-with-a-variable – NixonSparrow Sep 14 '22 at 11:34
  • 1
    @NixonSparrow you can get values from dictionaries with just the dot operator, see this [documentation](https://docs.djangoproject.com/en/4.1/ref/templates/language/#variables). Although I do agree this answer isn't useful as you don't need to have an extra dictionary... – Abdul Aziz Barkat Sep 14 '22 at 11:58