[Answered ]-Django – Pass Object ID after redirect

1👍

You can define 2 URLs, one with an optional sprint ID:

urls.py:

path('story/<int:sprint_id>/add/', views.create_story, name='create_story_for_sprint'),
path('story/add/', views.create_story, name='create_story_for_backlog'),

views.py:

def create_story(request, sprint_id=None):
   ...
   form = CreateNewUserStory(response.POST, initial={"sprint_id": sprint_id})
   if form.is_valid():
       ...
       sprint_id = form.cleaned_data["sprint_id"]

create_story.html:

<form action="..." method="POST">
    <input type="hidden" name="sprint_id" value="{{ sprint_id|default_if_none:'' }}">
    ...

Then, in your sprint page, use

<a href="{% url 'create_story_for_sprint' sprint.id %}">Add Story</a>

and in your backlog page:

<a href="{% url 'create_story_for_backlog' %}">Add Story</a>
👤Selcuk

Leave a comment