This is my url:
url(r'^add/(?P<year>[0-9]+)/(?P<month>[0-9]+)/(?P<day>[0-9]+)/(?P<id>[0-9]*)$', views.AddWorkoutView.as_view(), name = 'add_workout'),
It is for adding workouts to the database. If the id is provided, the server should:
- Check if a workout with the id exists in database
- If it exists, destroy the found item and replace it with the one who's data has been posted to the view.
- If it doesn't exist, simply add the new workout to the database without destroying any existing workout object.
In one of my templates, called addworkout.html, I have a form where the workout data is inserted. It has the following opening tag:
<form id="workoutform" action="{% url 'workoutcal:add_workout' date.year date.month date.day %}" method="post">
As you can see, I haven't provided any id argument in the reverse url. This is because in addworkout.html the user can only add new workouts, so no existing workout id is necessary. However, this attempt at reversing the url generates an error:
NoReverseMatch at /workoutcal/add/2018/1/2/
Reverse for 'add_workout' with arguments '(2018, 1, 2)' not found. 1 pattern(s) tried: ['workoutcal/add/(?P<year>[0-9]+)/(?P<month>[0-9]+)/(?P<day>[0-9]+)/(?P<id>[0-9]*)$']
Since I have written a regex star next to the id number in the url, there shouldn't be any requirement to provide id. The only explanation I can think of is that the last forward slash is the culprit, maybe it isn't generated when reversing the url.
If my explanation is correct, how do I deal with this trailing slash problem the best way?