I am building an event management app using Django. I have created a dynamic calendar, and I'm trying to add a link to next months calendar in my navigation.
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name = 'home'),
path('<int:year>/<str:month>/', views.home),
]
In my views.py file I added parameters like so:
def home(request, year=datetime.now().year, month=datetime.now().strftime('%B')):
<li class="nav-item">
<a class="nav-link" href="{% url 'home' '2021' 'November' %}">November</a>
</li>
When I add the two arguments, I get a template rendering error:
Reverse for 'home' with arguments '('2021', 'November')' not found. 1 pattern(s) tried: ['$']
When I remove the two arguments and only leave 'home' the template error goes away, and I'm not sure why. Any help is appreciated!
CodePudding user response:
It seems like your URLconf doesn't specify your two arguments (correctly). It should probably look something like this:
from django.urls import path
from . import views
urlpatterns = [
path('/<int:year>/<str:month>/', views.home, name='home'),
]
CodePudding user response:
What you have here are two different urls which happen to be handled by the same view. The first url is your home page with no date specified, and the second is your page with a year and month specified.
Handle these as two differently named url patterns, eg:
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home'),
path('<int:year>/<str:month>/', views.home, name='byyearmonth'),
]
Template:
<li class="nav-item">
<a class="nav-link" href="{% url 'byyearmonth' '2021' 'November' %}">November</a>
</li>