Hi,
I’m building my first web app with Django. It’s destined to display a catalog of relatively short monographies, called notices, of antique architecture objects.
There’s an list view that shows the clickable title of the 40 lasts recorded notices, and a detail view to display the notice itself: title, record date, link to pictures, etc.
While certain notices display as expected, I’ve got an error that I can’t figure out when I click on certain others. I checked in the database but didn’t find the answer yet.
Here is my urls.py so far:
from django.urls import path, include
from . import views
from notices.models import ObjetArchi
app_name = 'notices'
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
path('<int:pk>', views.DetailView.as_view(), name='detail'),
]
and my views.py:
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
from django.urls import reverse
from django.views import generic
from notices.models import ObjetArchi
class IndexView(generic.ListView):
template_name = 'notices/index.html'
context_object_name = 'dernier_oa_liste'
def get_queryset(self):
return ObjetArchi.objects.order_by('-date')[:40]
class DetailView(generic.DetailView):
model = ObjetArchi
template_name = 'notices/detail.html'
My 3 HTML templates:
base.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>
OrAG{% block title %}{% endblock %}
</title>
</head>
<body>
<header>
{% block headerContent %}{% endblock %}
</header>
<section id="content">
{% block content %}{% endblock %}
</section>
</body>
</html>
index.html:
{% extends "notices/base.html" %}
{% block title %} - Accueil{% endblock %}
{% block content %}
{% block bodyId %}Dernières notices saisies :{% endblock %}
{% if dernier_oa_liste %}
<ul>
{% for objetarchi in dernier_oa_liste %}
<li><a href="{% url 'notices:detail' objetarchi.id %}">{{ objetarchi.titre }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>Aucune notice n'est accessible.</p>
{% endif %}
{% endblock %}
and detail.html:
{% extends "notices/base.html" %}
{% block title %} - Notices{% endblock %}
{% comment %} {% block bodyId %}notices{% endblock %}
{% block headerContent %}Ornementation architecturale des Gaules {% endblock %} {% endcomment %}
{% block content %}
<h1>{{ object.titre }}</h1>
<p><i>Notice n°{{ object.reference }}, publiée le {{ object.date }}.</i></p>
<p>{{ object.resume }}</p>
{% comment %} (more content here) {% endcomment %}
{% endblock %}
And finally here is the error that shows up when trying to show some notices (not all):
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/notices/101
Django Version: 3.1.4
Python Version: 3.8.6
Installed Applications:
['notices.apps.NoticesConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Template error:
In template /home/jeremy/amubox/code/orag/notices/templates/notices/base.html, error at line 0
Field 'id' expected a number but got ''.
1 : <!DOCTYPE html>
2 : <html>
3 : <head>
4 : <meta charset="utf-8">
5 : <title>
6 : OrAG{% block title %}{% endblock %}
7 : </title>
8 : </head>
9 : <body>
10 : <header>
Traceback (most recent call last):
File "/home/jeremy/.local/lib/python3.8/site-packages/django/template/base.py", line 829, in _resolve_lookup
current = current[bit]
During handling of the above exception ('ObjetArchi' object is not subscriptable), another exception occurred:
File "/home/jeremy/.local/lib/python3.8/site-packages/django/db/models/fields/related_descriptors.py", line 173, in __get__
rel_obj = self.field.get_cached_value(instance)
File "/home/jeremy/.local/lib/python3.8/site-packages/django/db/models/fields/mixins.py", line 15, in get_cached_value
return instance._state.fields_cache[cache_name]
During handling of the above exception ('provinceantique'), another exception occurred:
File "/home/jeremy/.local/lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 1774, in get_prep_value
return int(value)
The above exception (invalid literal for int() with base 10: '') was the direct cause of the following exception:
File "/home/jeremy/.local/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/home/jeremy/.local/lib/python3.8/site-packages/django/core/handlers/base.py", line 202, in _get_response
response = response.render()
File "/home/jeremy/.local/lib/python3.8/site-packages/django/template/response.py", line 105, in render
self.content = self.rendered_content
File "/home/jeremy/.local/lib/python3.8/site-packages/django/template/response.py", line 83, in rendered_content
return template.render(context, self._request)
File "/home/jeremy/.local/lib/python3.8/site-packages/django/template/backends/django.py", line 61, in render
return self.template.render(context)
File "/home/jeremy/.local/lib/python3.8/site-packages/django/template/base.py", line 170, in render
return self._render(context)
File "/home/jeremy/.local/lib/python3.8/site-packages/django/template/base.py", line 162, in _render
return self.nodelist.render(context)
File "/home/jeremy/.local/lib/python3.8/site-packages/django/template/base.py", line 938, in render
bit = node.render_annotated(context)
File "/home/jeremy/.local/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated
return self.render(context)
File "/home/jeremy/.local/lib/python3.8/site-packages/django/template/loader_tags.py", line 150, in render
return compiled_parent._render(context)
File "/home/jeremy/.local/lib/python3.8/site-packages/django/template/base.py", line 162, in _render
return self.nodelist.render(context)
File "/home/jeremy/.local/lib/python3.8/site-packages/django/template/base.py", line 938, in render
bit = node.render_annotated(context)
File "/home/jeremy/.local/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated
return self.render(context)
File "/home/jeremy/.local/lib/python3.8/site-packages/django/template/loader_tags.py", line 62, in render
result = block.nodelist.render(context)
File "/home/jeremy/.local/lib/python3.8/site-packages/django/template/base.py", line 938, in render
bit = node.render_annotated(context)
File "/home/jeremy/.local/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated
return self.render(context)
File "/home/jeremy/.local/lib/python3.8/site-packages/django/template/base.py", line 988, in render
output = self.filter_expression.resolve(context)
File "/home/jeremy/.local/lib/python3.8/site-packages/django/template/base.py", line 671, in resolve
obj = self.var.resolve(context)
File "/home/jeremy/.local/lib/python3.8/site-packages/django/template/base.py", line 796, in resolve
value = self._resolve_lookup(context)
File "/home/jeremy/.local/lib/python3.8/site-packages/django/template/base.py", line 837, in _resolve_lookup
current = getattr(current, bit)
File "/home/jeremy/.local/lib/python3.8/site-packages/django/db/models/fields/related_descriptors.py", line 187, in __get__
rel_obj = self.get_object(instance)
File "/home/jeremy/.local/lib/python3.8/site-packages/django/db/models/fields/related_descriptors.py", line 154, in get_object
return qs.get(self.field.get_reverse_related_filter(instance))
File "/home/jeremy/.local/lib/python3.8/site-packages/django/db/models/query.py", line 418, in get
clone = self._chain() if self.query.combinator else self.filter(*args, **kwargs)
File "/home/jeremy/.local/lib/python3.8/site-packages/django/db/models/query.py", line 942, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "/home/jeremy/.local/lib/python3.8/site-packages/django/db/models/query.py", line 962, in _filter_or_exclude
clone._filter_or_exclude_inplace(negate, *args, **kwargs)
File "/home/jeremy/.local/lib/python3.8/site-packages/django/db/models/query.py", line 969, in _filter_or_exclude_inplace
self._query.add_q(Q(*args, **kwargs))
File "/home/jeremy/.local/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1358, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "/home/jeremy/.local/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1377, in _add_q
child_clause, needed_inner = self.build_filter(
File "/home/jeremy/.local/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1237, in build_filter
return self._add_q(
File "/home/jeremy/.local/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1377, in _add_q
child_clause, needed_inner = self.build_filter(
File "/home/jeremy/.local/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1319, in build_filter
condition = self.build_lookup(lookups, col, value)
File "/home/jeremy/.local/lib/python3.8/site-packages/django/db/models/sql/query.py", line 1165, in build_lookup
lookup = lookup_class(lhs, rhs)
File "/home/jeremy/.local/lib/python3.8/site-packages/django/db/models/lookups.py", line 24, in __init__
self.rhs = self.get_prep_lookup()
File "/home/jeremy/.local/lib/python3.8/site-packages/django/db/models/lookups.py", line 76, in get_prep_lookup
return self.lhs.output_field.get_prep_value(self.rhs)
File "/home/jeremy/.local/lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 1776, in get_prep_value
raise e.__class__(
Exception Type: ValueError at /notices/101
Exception Value: Field 'id' expected a number but got ''.
Any help would be very much appreciated!
I can post extracts of the database if needed to compare data.
Thanks a lot!