Raits
Raits

Reputation: 95

Getting ValuError: Field 'id' expected a number but got a html

I am a newcomer, about 10 days into a self-taught Django, and I am building a Django (v3.2) app. The main view in the app is a list of countries (view: CountryListView, template: countries_view.html). When I click on a country in that list, a detail view is brought up to see detailed country data (CountryDetailView and country_detail.html). In CountryDetailView, there are navigation buttons: either back to CountryListView, or 'Edit', which shall bring the user to CountryEditView, where the country parameters can be changed & saved.

However, when I click on 'Edit', I get the following error:

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'

I am guessing this might to do something with values returned (or rather expected but not returned) from CountryDetailView, but what they are? And how to make CountryDetailView to return the object id? (I am using plain integer id's in my model)

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, with two navigation buttons (back to the list), and further to Edit form (this is the one that does not work).

{% 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 %}

Upvotes: 1

Views: 162

Answers (1)

NKSM
NKSM

Reputation: 5854

Your onclick attribute of button contains invalid url:

<button>onclick="window.location.href='../country_edit.html';">Edit</button>

Use instead template tag url (Django Docs):

<button class="btn btn-secondary mt-3" onclick="window.location.href='{% url 'country_edit' object.pk %}';">Edit</button>

Upvotes: 1

Related Questions