Is it possible to attach a string variable in return redirect() in django
I have a situation here where I am creating a record from a view
def index(request):
if request.method == "POST":
order_number = request.POST.get('order_number')
post_code = request.POST.get('post_code')
print(order_number)
print(post_code)
if order_number and post_code:
n = page1.objects.create(orderNumber=order_number, postCode=post_code)
n.save()
id = n.id
return redirect("page1:waiting")
return render(request,"page1/index.html")
And upon creating the record I am redirecting the user to another page with id.
127.0.0.1/page1/waiting/fb65-io98-.....
from django.urls import path
from . import views
app_name = 'page1'
urlpatterns = [
path('', views.index, name="amounts"),
path('waiting/<str:pk>', views.waitReply, name="waiting"),
]
View for Second URL
def waitReply(request,pk):
get_amount = page1.objects.get(id=pk)
context = {'get_amount':get_amount}
return render(request, "page1/payment-confirm.html", context)
But the problem is how do I attach/generate/get the id in index
view and send it for the waitReply
view
I can get the latest ID through n.id
Kindly help.
Use reverse with args in HttpResponseRender.
return HttpResponseRender(reverse('page1:waiting' args=[id]))