1

I have url(r'^topics/(?P<topic_id>\d+)/$', views.topic, name='topic') in urls.py but when I try to go to localhost:8000/topics/1 it tells me that it tried one pattern: topics/(?P<topic_id>**\\**d+)/$ I would think it would be topics/(?P<topic_id>**\**d+)/$

I'm using a book called The Python Crash Course (1st edition)(ch. 18). This is a local server using Django 1.11 with Python. I've tried a lot of reformatting on the url pattern but I am new at this so I don't know what else to do.

...
urlpatterns = [
    url(r'^$', views.index, name='index'),

    # Show all topics.
    url(r'^topics/$', views.topics, name='topics'),

    # Detail page for a single topic.
    url(r'^topics/(?P<topic_id>\d+)/$', views.topic, name='topic'),
]

I expected it to pop up with the correct page but it always says 'NoReverseMatch at /topics/01/'

hugo
  • 3,067
  • 2
  • 12
  • 22

1 Answers1

1

So you've forgotten the trailing slash at the end of your URL, hence why it's not matching.

You could remove the slash from the regex, but that would shift the problem: it wouldn't work if you put a slash.

I guess you could end the pattern with /?$, but here's a solution that's probably more robust: Jiaaro's answer to: django urls without a trailing slash do not redirect

Basically:

check your APPEND_SLASH setting in the settings.py file

hugo
  • 3,067
  • 2
  • 12
  • 22