通过django中的URL传递列表

时间:2016-02-18 14:03:05

标签: python django python-2.7 django-views django-urls

我想通过网址传递一个列表。但是当我尝试时,我遇到了一些错误。那我怎么能这样做呢。有人请帮帮我..

这是我的观点

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)

这是我的网址:

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

这是我得到的错误。

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>.*)/$']

这里(14,15)是列表项目。

如果问题不正确。有人请纠正这个问题。

1 个答案:

答案 0 :(得分:2)

如果您只想渲染一个列表,那么您应该这样做。

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')

但是,您的问题似乎与用户双重发布此请求有关。要解决此问题,您可以将它们作为get参数传递

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)