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
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:
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 thedashboard
view at the/dashboard/
URL.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:you can also name your dictionary context and pass it in render like: