Getting ValuError: Field 'id' expected a number but got a html
Getting ValuError: Field 'id' expected a number but got a html
我是一名新手,自学 Django 大约 10 天,我正在构建 Django (v3.2) 应用程序。
应用程序中的主视图是国家列表(视图:CountryListView,模板:countries_view.html)。
当我单击该列表中的一个国家/地区时,会弹出一个详细视图以查看详细的国家/地区数据(CountryDetailView 和 country_detail.html)。
在 CountryDetailView 中,有导航按钮:返回 CountryListView,或 'Edit',这会将用户带到 CountryEditView,可以在其中更改和保存国家/地区参数。
但是,当我点击 'Edit' 时,出现以下错误:
Request Method: GET
Request URL: http://127.0.0.1:8000/manage_countries/countries/country_edit.html/
Django Version: 3.2.4
Exception Type: ValueError
Exception Value: Field 'id' expected a number but got 'country_edit.html'
我猜这可能与 CountryDetailView 的值 returned(或者更确切地说是预期但不是 returned)有关,但它们是什么?以及如何将 CountryDetailView 设置为 return 对象 ID? (我在我的模型中使用普通整数 ID)
views.py
class CountryListView(LoginRequiredMixin, ListView):
model = Countries
context_object_name = 'countries_list'
template_name = 'manage_countries/countries_view.html'
class CountryDetailView(LoginRequiredMixin, DetailView):
model = Countries
template_name = 'manage_countries/country_detail.html'
class CountryEditView(LoginRequiredMixin, UpdateView):
model = Countries
template_name = 'manage_countries/country_edit.html'
success_url = reverse_lazy('manage_countries:countries_view')
urls.py
path('', CountryListView.as_view(),name='countries_view'),
path('countries/<pk>/', CountryDetailView.as_view(), name='country-detail'),
path('<pk>/edit', CountryEditView.as_view(), name='country_edit'),
countries_view.html
{% block content %}
<div class="list-group col-6">
<a href="country_add.html" class="list-group-item list-group-item-action shadow-mt list-group-flush list-group-item-dark text-light">Click here to add country data</a>
{% for country in countries_list %}
<a href="{{ country.get_absolute_url }}" class="list-group-item list-group-item-action shadow-mt list-group-flush list-group-item-light"><small><span class="text-dark">{{ country.name }}</span></small></a>
{% endfor %}
</div>
{% endblock content %}
country_detail.html,有两个导航按钮(返回列表),进一步到编辑表单(这是不起作用的那个)。
{% block content %}
<div class="card col-5 shadow-mt">
<h5 class="card-header bg-light text-center">Country data</h5>
<div class="card-body">
<table class="table">
<thead><tr>
<th scope="col"><small>Name: </small></th>
<th scope="col"><small>{{ object.name }}</small></th>
</tr></thead>
</table>
<button class="btn btn-secondary mt-3" onclick="javascript:history.back();">Back</button>
<button class="btn btn-secondary mt-3" onclick="window.location.href='../country_edit.html';">Edit</button>
</div>
</div>
{% endblock content %}
您的 onclick
按钮属性包含无效 url:
<button>onclick="window.location.href='../country_edit.html';">Edit</button>
改用模板标签 url
(Django Docs):
<button class="btn btn-secondary mt-3" onclick="window.location.href='{% url 'country_edit' object.pk %}';">Edit</button>
我是一名新手,自学 Django 大约 10 天,我正在构建 Django (v3.2) 应用程序。 应用程序中的主视图是国家列表(视图:CountryListView,模板:countries_view.html)。 当我单击该列表中的一个国家/地区时,会弹出一个详细视图以查看详细的国家/地区数据(CountryDetailView 和 country_detail.html)。 在 CountryDetailView 中,有导航按钮:返回 CountryListView,或 'Edit',这会将用户带到 CountryEditView,可以在其中更改和保存国家/地区参数。
但是,当我点击 'Edit' 时,出现以下错误:
Request Method: GET
Request URL: http://127.0.0.1:8000/manage_countries/countries/country_edit.html/
Django Version: 3.2.4
Exception Type: ValueError
Exception Value: Field 'id' expected a number but got 'country_edit.html'
我猜这可能与 CountryDetailView 的值 returned(或者更确切地说是预期但不是 returned)有关,但它们是什么?以及如何将 CountryDetailView 设置为 return 对象 ID? (我在我的模型中使用普通整数 ID)
views.py
class CountryListView(LoginRequiredMixin, ListView):
model = Countries
context_object_name = 'countries_list'
template_name = 'manage_countries/countries_view.html'
class CountryDetailView(LoginRequiredMixin, DetailView):
model = Countries
template_name = 'manage_countries/country_detail.html'
class CountryEditView(LoginRequiredMixin, UpdateView):
model = Countries
template_name = 'manage_countries/country_edit.html'
success_url = reverse_lazy('manage_countries:countries_view')
urls.py
path('', CountryListView.as_view(),name='countries_view'),
path('countries/<pk>/', CountryDetailView.as_view(), name='country-detail'),
path('<pk>/edit', CountryEditView.as_view(), name='country_edit'),
countries_view.html
{% block content %}
<div class="list-group col-6">
<a href="country_add.html" class="list-group-item list-group-item-action shadow-mt list-group-flush list-group-item-dark text-light">Click here to add country data</a>
{% for country in countries_list %}
<a href="{{ country.get_absolute_url }}" class="list-group-item list-group-item-action shadow-mt list-group-flush list-group-item-light"><small><span class="text-dark">{{ country.name }}</span></small></a>
{% endfor %}
</div>
{% endblock content %}
country_detail.html,有两个导航按钮(返回列表),进一步到编辑表单(这是不起作用的那个)。
{% block content %}
<div class="card col-5 shadow-mt">
<h5 class="card-header bg-light text-center">Country data</h5>
<div class="card-body">
<table class="table">
<thead><tr>
<th scope="col"><small>Name: </small></th>
<th scope="col"><small>{{ object.name }}</small></th>
</tr></thead>
</table>
<button class="btn btn-secondary mt-3" onclick="javascript:history.back();">Back</button>
<button class="btn btn-secondary mt-3" onclick="window.location.href='../country_edit.html';">Edit</button>
</div>
</div>
{% endblock content %}
您的 onclick
按钮属性包含无效 url:
<button>onclick="window.location.href='../country_edit.html';">Edit</button>
改用模板标签 url
(Django Docs):
<button class="btn btn-secondary mt-3" onclick="window.location.href='{% url 'country_edit' object.pk %}';">Edit</button>