[Fixed]-Invalid literal for int() with base 10: 'new'

0👍

You should learn to read the traceback. Here is the error:

car = get_object_or_404(Car, pk=pk)

and the error happens in:

File "C:\Users\name\djangofolder\myproject\website\views.py" in car_detail

You passed the pk in url as a string new, but your car_detail method is expecting an integer which represents the pk of Car. If you read the error message again it should make more sense.

1👍

The url for /car/new/ is resolving to your car_detail view, instead of the car_new view.

You haven’t shown your url patterns, so I can’t give a precise answer, but you can probably fix the problem by doing either of the following:

  1. Move the car_new url above the car_detail view.
  2. Change the regex for the primary key so that it matches only digits (at the moment, you are probably using something like \w+, which matches strings like new.

Putting that together, you something like:

url(r'^car/new/$', views.car_new, name='car_new'),
url(r'^car/(?P<pk>\d+)/$', views.car_detail, name='car_detail'),

Leave a comment