Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
409 views
in Technique[技术] by (71.8m points)

python - Passing a list through url in django

I want to pass a list through the url. But when i tried, i got some errors. So how can i do that. Somebody please help me..

this is my view

def add_student(request):

    if request.method == 'POST':
        student_list = []
        student_name = request.POST.getlist('student_name')
        student_phone = request.POST.getlist('student_phone')

        zipped = zip(student_name,student_phone)

        for student_name,student_phone in zipped:

            student_object = Student(
                                student_name=student_name,
                                student_phone=student_phone
                            )
            student_object.save()

            student_list.append(student_object.id)

        return HttpResponseRedirect(reverse('students:view_students', args=student_list))
**# in the above code it throwing some errors** 
    else:
        return render(request,'students/add_student.html')


def view_students(request,student_list=None):
    if student_list:
        instances = Student.objects.filter(id__in=student_list)
    else:
        instances = Student.objects.filter()
    context = {}
    context['instances'] = instances

    return render(request,'students/view_all_student.html',context)

this is my url :

url(r'^view-students/(?P<student_list>w+)/$',views.view_students, name='view_students'),

this is the error i got.

NoReverseMatch at /app/product/add-product/

Reverse for 'view_products' with arguments '(14, 15)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'app/product/view-products/(?P<pd_list>.*)/$']

here (14,15) are the list items.

If the question is not correct. Somebody please correct the question.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If all you want to do is render a list you should just do that.

def add_student(request):
    if request.method == 'POST':
        student_list = []
        student_name = request.POST.getlist('student_name')
        student_phone = request.POST.getlist('student_phone')

        zipped = zip(student_name,student_phone)

        for student_name,student_phone in zipped:
            student = Student.objects.create(student_name=student_name,
                                             student_phone=student_phone)
            student_list.append(student)

        return render(request,'students/view_all_student.html', {'instances': student_list})
    else:
        return render(request,'students/add_student.html')

However, Your issue seems to be concerned with users double posting this request. To remedy this you could pass them as a get parameter

def add_student(request):
    if request.method == 'POST':
        student_list = []
        student_name = request.POST.getlist('student_name')
        student_phone = request.POST.getlist('student_phone')

        zipped = zip(student_name,student_phone)

        for student_name,student_phone in zipped:
            student = Student.objects.create(student_name=student_name,
                                             student_phone=student_phone)
            student_list.append(str(student.id))

        redirect = HttpResponseRedirect(reverse('students:view_students'))
        redirect['Location'] += '&'.join(['students={}'.format(x) for x in student_list]))
        return redirect
    else:
        return render(request,'students/add_student.html')

def view_students(request):
    students = request.GET.getlist('students')
    if students:
        students = [int(x) for x in students]
        instances = Student.objects.filter(id__in=students)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...