Internal server error: /favicon.ico/

Hello,

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?

Best,
Artur

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.

Hello Ken,

Thank you. This is my urls.py:

from django.urls import path
from . import views

urlpatterns = [
    path("", views.cities_index, name="cities_index"),
    path("<city_name>/", views.hotel_results, name="hotel_results"),
]

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.

These are my views:

def cities_index(request):
    cities = Cities.objects.all()
    context = {
        'cities': cities
    }
    return render(request, 'cities_index.html', context)

def hotel_results(request, city_name):
    city = Cities.objects.get(city_name=city_name)
    hotels = city.hotels_set.all()
    context = {
        'hotels': hotels,
        'city': city
    }
    return render(request, 'hotel_results.html', context)

And the index template:

{% extends "base.html" %}

{% block page_content %}

<div class="col-md-8 offset-md-2">
    <h1>Find a hotel to stay</h1>
    <div class="dropdown">
        <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
            Select a city
        </button>
        <div class="dropdown-menu">
            {% for city in cities %}
                <a class="dropdown-item" href="{% url 'hotel_results' city.city_name %}">{{ city.city_name }}</a>
            {% endfor %}
        </div>
    </div>
</div>

{% endblock %}

How could I get the city name in the url path when the user selects a city without this causing an error or is that just not advisable at all?

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>/’

1 Like

Hi Ken,

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.

1 Like

No, they are not.

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/.

Okay, yes I see how that is different from having polls/ at the top level.