Home > Software engineering >  The new url id is getting appended after the previous request's url
The new url id is getting appended after the previous request's url

Time:05-16

I'm making a get request in django using html form as:

crud.html

<form action="{% url 'crudId' id %}" method="get">
                <div >
                    <div  style="width: 20%;margin-top: 5px">
                        <label for="name">Id</label>
                        <input type="number" name="id" value="{% if data %}{{ id }}{% endif %}">
                    </div>
                    <input type="submit" value="OK" style="width: 30%; margin-left: 40%">
                    ...

my view for this:

class CrudPageView(View):

    def get(self, request, id=1):
        # get the id from the page
        oid = id
        ...
        # some data and operation
        ...
        return render(request, 'crud.html', {'data': data, 'id': oid})

and in urls.py as :

 path('crud/', CrudPageView.as_view(), name='crud'),
 path('crud/id=<int:id>', CrudPageView.as_view(), name='crudId')
 ...

so http://localhost:8000/crud/ does work as intended and shows the page with data of id = 1; when I input any id, it also works as intended and shows the data for that id, but the url is in the form http://localhost:8000/crud/id=1?id=5 and after another input it will be http://localhost:8000/crud/id=5?id=12

What am I doing wrong?

CodePudding user response:

Try something like this:

def index(request, id) -> HttpResponse:
    # some data

    return render(request, 'websites/index.html', context={'data': data, 'id': id})


def get(request) -> HttpResponse:
    id_number = request.GET.get('number')
    # some data

    return redirect(index, id=id)
  • Related