skip to Main Content

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

2

Answers


  1. It seems like there might be an issue with your URL patterns. You have both paths set to dashboard/. This can lead to conflicts, and the second path might be overriding the first one.

    Try modifying your URLs like this:

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

    Make sure you also update any links or references in your templates accordingly. After making these changes, you should be able to access the dtasks_24 view at the /dtasks_24/ URL, and the dashboard view at the /dashboard/ URL.

    Login or Signup to reply.
  2. 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)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search