我在django中使用html形式的get请求:
crud.html
<form action="{% url 'crudId' id %}" method="get">
<div class="col">
<div class="form-group" 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%">
...
我的观点:
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})
和url .py中的
path('crud/', CrudPageView.as_view(), name='crud'),
path('crud/id=<int:id>', CrudPageView.as_view(), name='crudId')
...
所以http://localhost:8000/crud/确实按预期工作,并显示了数据id = 1的页面;当我输入任何id时,它也按预期工作并显示该id的数据,但url的形式是http://localhost:8000/crud/id=1?id=5
,在另一个输入后,它将是http://localhost:8000/crud/id=5?id=12
我做错了什么?
试试这样:
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)