I try to pass a list through an URL in Django. I found this: Passing a list through url in django But I still get Errors. I feel like Iam running in a circle.
My urls:
path('query/', include(('query.urls', 'query-space'), namespace='query-space')),
re_path(r'^2/(?P<amb_list>\w+)/$',views.ambitionGenPage, name='userambitiongen'),
My views:
def ambitionPage(request):
if request.method == 'POST':
form = AmbitionForm(request.POST)
if form.is_valid():
ambs_list = form.cleaned_data['ambition_field']
redirect = HttpResponseRedirect(reverse('query-space:userambitiongen'))
redirect['Location'] += '&'.join(['ambs={}'.format(x) for x in ambs_list])
return redirect
form = AmbitionForm()
return render(request, 'query/ambition.html',{'form':form,})
def ambitionGenPage(request):
ambitions = request.GET.getlist('amb_list')
if ambitions:
ambitions = [int(x) for x in ambitions]
print(ambitions) #I first want to check what data I get
return render(request, 'query/ambitionGen.html',{})
I adapted the code of the link.
In the line:
redirect = HttpResponseRedirect(reverse('query-space:userambitiongen', args=(amb_list)))
he doesnt know the argument:
NameError: name 'amb_list' is not defined
In the example there is no argument. When I try this I get the error:
Reverse for 'userambitiongen' with no arguments not found. 1 pattern(s) tried: ['query/2/(?P<amb_list>\\w+)/$']
I also found nothing in the internet to this expression: redirect['Location']
Could someone explain to me what ['Location'] stands for?
What would be the right solution? I tried to find it by myself in many hours.
Thank you very much for your time!