python - from django.urls import path
Pasar una lista a través de la url en django (1)
Quiero pasar una lista a través de la url. Pero cuando lo intenté, obtuve algunos errores. Entonces, ¿cómo puedo hacer eso? Alguien por favor ayudame ..
esta es mi vista
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)
esta es mi url:
url(r''^view-students/(?P<student_list>/w+)/$'',views.view_students, name=''view_students''),
este es el error que obtuve
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>.*)/$'']
aquí (14,15) son los artículos de la lista.
Si la pregunta no es correcta Alguien por favor corrige la pregunta.
Si todo lo que quiere hacer es generar una lista, simplemente debe hacer eso.
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'')
Sin embargo, su problema parece estar relacionado con la publicación doble de esta solicitud por parte de los usuarios. Para remediar esto, podría pasarlos como un parámetro 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)