简体   繁体   中英

Pass URL parameter to form value Django

I'm trying to set the value of a form input to the url, then redirecting to another page with another form using the information in the url parameter and passing another value of another input to the url.

Let's say that my url is page1/value1/page2/value2

Where value1 comes from the first form and value2 from the second form in the second page.

Using this code I can get the first value in the url, but somehow I can't save the form information to the database and set the other value in the url.

class Acceso(FormView):
    template_name = 'codigo_zona.html'
    form_class = AccesoForm

    def form_valid(self, form):
        zona_code = form.cleaned_data.get('codigo_acceso')
        return redirect('registro', zona_code)


class AccesoDetails(View):
    def get(self, request, zona_code):
        zona = Zona.objects.filter(codigo_acceso=zona_code).first()

        if request.method == 'POST':
            zona_id = zona.pk
            range_start = 10 ** (6 - 1)
            range_end = (10 ** 6) - 1
            access_code = randint(range_start, range_end)
            form = RegistroResidenteForm(request.POST)
            if form.is_valid():
                registro_residente = form.save(commit=False)
                registro_residente.zona_residencial.pk = zona_id
                registro_residente.codigo_acceso = access_code
                registro_residente.status_activacion = False
                registro_residente.save()
        else:
            form = RegistroResidenteForm()

        context = {
            'zona': zona,
            'form': form
        }
        return render(request, 'registro_residentes.html', context)

I get the error Method not allowed (Post): HTTP/1.1 405 0 when submiting the second form, so I never see the second value in the url.

You are using POST method in get method please change the method name to "post". Even you don't need to check the method. Here is correct code.

    def post(self, request, zona_code):
        zona = Zona.objects.filter(codigo_acceso=zona_code).first()
        zona_id = zona.pk
        range_start = 10 ** (6 - 1)
        range_end = (10 ** 6) - 1
        access_code = randint(range_start, range_end)
        form = RegistroResidenteForm(request.POST)

        if form.is_valid():
            registro_residente = form.save(commit=False)
            egistro_residente.zona_residencial.pk = zona_id
            registro_residente.codigo_acceso = access_code
            registro_residente.status_activacion = False
            registro_residente.save()
        else:
            form = RegistroResidenteForm()

        context = {
            'zona': zona,
            'form': form
        }
        return render(request, 'registro_residentes.html', context)

Django generic view (View) is work with get method and form submitting is post method, that's why django raising error, you can use CreateView or FormView also you can use function based view instead of using Class Based View

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM