简体   繁体   中英

Django URL invalid literal for int() with base 10

I have a Location model setup in Django and I originally set it up so that the URL was /location/3/ using the primary key. but the client want to change it to use a slug of the location name. I created a slug field in the model. And changed the URL pattern in urls.py.

urlpatterns = [
    url(r'^(?P<pk>[\w-]+)/$', views.LocationsSingleView.as_view(), name='detail'),
]

I can link to them fine. But when it tries to actually load that page I get an error:

invalid literal for int() with base 10: 'eau-claire'

Where eau-claire is the slug for that Location.

Documentation doesn't say anything about this.

A lot of people with this issue have issues with their view. But my view is super simple. So I don't know what's wrong. Here is my view:

class LocationsSingleView(DetailView):
    model = models.Location

您需要在网址格式中使用slug而不是pk来告诉Django您要通过该slug查找。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM