1

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!

  • Isn't it just due to a typo? You have a variable `ambs_list` and it can't find the variable `amb_list`, which indeed does not exist. – physicalattraction Nov 15 '20 at 12:24
  • Oha that was the cause. Thank you! Now I got the error that an example list of ambs_list doesnt fit to the regex in my URL: ```Reverse for 'userambitiongen' with arguments '('FR', 'FP')' not found. 1 pattern(s) tried: ['query/2/(?P\\w+)/$']```I took over the URL of the questioner of the link, because there were no other URL. Sadly that didnt work. Do you know how the regex should be? I also didnt understand what is happening in the two lines beginning with 'redirect'. I would be very thankfull for an explanation. I hope I dont just act too stupid – August Graf Nov 15 '20 at 13:55
  • In that case, you have to be more specific about the url files you are using (not just excerpts from them, and not just one of them), in order for someone else to check what could be going on. – physicalattraction Nov 15 '20 at 15:25

1 Answers1

0

return redirect['Location']
Redirects you to example: yourwebpage.com/Location

You can save the list into a sql database temporarily and later delete it.

ville3
  • 1
  • 2
  • Ahh thank you for the explanation! I got this idea with saving it in the database first to, but than I thought it would be much faster to transfer the list in the URL, than load it in the db, just to query the list in the next view out of the db, where I need it next. Or am I false and the time difference between the two solutions is insignificant? – August Graf Nov 15 '20 at 13:32
  • Depends on the workload, database is an very effective way to store and pull lots data. But if its something small, then URL should be fine too. – ville3 Nov 16 '20 at 15:56