django.core.exceptions.ImproperlyConfigured: uses parameter name 'order.id' which isn't a valid Python identifier

Question:

when i’m running the command python manage.py runserver i’m getting an error:

django.core.exceptions.ImproperlyConfigured: URL route 'order_detail/<int:order.id>/' uses parameter name 'order.id' which isn't a valid Python identifier.

views.py

def order_detail(request, order_id):
    if request.user.is_authenticated:
        email = str(request.user.email)
        order = Order.objects.get(id=order_id, emailAddress=email)
        order_items = OrderItem.objects.filter(order=order)
    return render(request, 'store/order_detail.html', {'order': order, 'order_items': order_items})

urls.py

urlpatterns = [
   path('', views.home, name='home')
   path('order/<int:order.id>/', views.order_detail, name='order_detail'),
]

template file detail.html

{% for order in order_details %}
<tr>
  <td>{{ order.id }}</td>
  <td>{{ order.created|date:"d M Y" }}</td>
  <td>{{ order.total }}</td>
  <td><i class="fas fa-check"></i>&nbsp;Complete</td>
  <td><a href="{% url 'order_detail' order.id %}">View Order</a></td>
</tr>
{% endfor %}
Asked By: tarun kumar

||

Answers:

order.id is not a valid python identifier, and thus not valid as parameter for route. ., - among others are not accepted characters in python identifiers.

it should be order_id as it’s defined in your view function order_detail(request, order_id)

change this route

   path('order/<int:order.id>/', views.order_detail, name='order_detail'),

to

   path('order/<int:order_id>/', views.order_detail, name='order_detail'),

and in your template it should be

<td><a href="{% url 'order_detail' order_id=order.id %}">View Order</a></td>
Answered By: cizario

You can simply change your path from this:
path(‘order/int:order.id/’, views.order_detail, name=’order_detail’)

To this:
path(‘order/int:order.id’, views.order_detail, name=’order_detail’)

Answered By: Undead Dev
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.