Even though my serves does what it’s supposed to be serving, I’m receiving this error in the console when starting opening the development server:
21/Dec/2022 17:07:41] "GET / HTTP/1.1" 200 1359
Internal Server Error: /favicon.ico/
Traceback (most recent call last):
File "/Users/arturschmal/Dropbox/maykin_case/.venv/lib/python3.11/site-packages/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
^^^^^^^^^^^^^^^^^^^^^
File "/Users/arturschmal/Dropbox/maykin_case/.venv/lib/python3.11/site-packages/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/arturschmal/Dropbox/maykin_case/hotels/views.py", line 13, in hotel_results
city = Cities.objects.get(city_name=city_name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/arturschmal/Dropbox/maykin_case/.venv/lib/python3.11/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/arturschmal/Dropbox/maykin_case/.venv/lib/python3.11/site-packages/django/db/models/query.py", line 650, in get
raise self.model.DoesNotExist(
hotels.models.Cities.DoesNotExist: Cities matching query does not exist.
[21/Dec/2022 17:07:42] "GET /favicon.ico/ HTTP/1.1" 500 68895
The error mentions the favicon but the traceback says the query does not exist. What could cause this error while the app does seem to be working as it should?
Something in your URLs is trying to pass “favicon.ico” as a parameter to a view. This is generally an indication that you’ve got a url defined with the first level as a parameter.
We’d need to see your urls.py file to start to diagnose this specifically.
That’s the reason for this problem - well, that and you apparently don’t have any error-checking in your view. (You would likely get this error if any invalid City name was supplied.)
You should always be prepared to handle invalid input in your urls. Beyond that, you generally don’t ever want to have a parameter as the first level of a url - it ends up being too restricting and potentially confusing of a url structure.
You must never trust input provided from the browser. You must always assume that any parameters coming from the browser might be invalid.
You have a couple different ways to catch those errors. For views built around a single get of a model, you have the get_object_or_404 method.
Or, you can wrap larger blocks of code in try / except blocks to catch a wider variety of errors.
It’s entirely up to you how you choose to handle situations like this.
My comment with the urls was more focused on the fact that you have that as the top level of the url. I would generally consider it more preferable to have the url defined (perhaps) as something like ‘city/<city_name>/’
Thank you for your feedback. I was a bit confused as this approach of calling a parameter in the url path is shown in the tutorial in the django documentation, but there they hard-coded a top level in the global urls.py.
I’ve implemented your suggestion of the get_object_or_404 method and the error message has disappeared. I will implement a different top level of the url as well.
The urls.py file in the polls app is not the top level of the url structure. That file is the mysite/urls.py file, which defines all urls in polls as being in a path starting with polls/.