Search code examples
htmldjangodjango-viewsdjango-templates

Using multiple variable in a Django HTML page?


Essentially I need to pass a few variables into the same HTML page in Django, yet I can only seem to register one.

Here is the View for the variable I want to use:

@login_required
def dtasks_24(request):
    usern = str(request.user)
    today = datetime.now().date()
    tomorrow = today + timedelta(days=1)
    tasks_due_tomorrow  = Task.objects.filter(assigned_members__username="@pjj",due_date__in=[today, tomorrow])
    dtasks_24 = tasks_due_tomorrow .count()
return render(request, 'dashboard.html', {'dtasks_24': dtasks_24})

Here is how I am using it in the dashboard.HTML file: {{ dtasks_24 }} Here are my URLs:

   path('dashboard/', views.dashboard, name='dashboard'),
   path('dashboard/', views.dtasks_24, name='dtasks_24'),

the variable dashboard work but dtasks_24 does not, I do not have a HTML file for dtasks_24.

Thank you for any help


Solution

  • Thanks to @Temi for his answer about urls. I wanted to add something about passing data to a view. You can send as much as data as you want to your view. in render(request, 'dashboard.html', {'dtasks_24': dtasks_24}) , {'dtasks_24': dtasks_24} is a dictionary. Usually it named 'context' and you can send as many as key values as you want to it Like:

    {'dtasks_24': dtasks_24,'new_key':'new_value','another_key':'another_value'}
    

    you can also name your dictionary context and pass it in render like:

    def dtask_24(request):
         #your code
         context = {'dtasks_24': dtasks_24,'new_key':'new_value','another_key':'another_value'}
    return render(request, 'dashboard.html', context)