B
B
BotaniQ_Q2017-07-13 15:41:06
Django
BotaniQ_Q, 2017-07-13 15:41:06

Dynamic urls not working in Django?

Reading Django book, doing an example from this page
https://rtfm.co.ua/django-book-trete-predstavlenie...
Here is my urls.py

from django.conf.urls import url
from django.contrib import admin
from views import hours_ahead


urlpatterns = [
    url(r'^admin/', admin.site.urls),
   	url(r'^time/plus/d+/$', hours_ahead),


]

here are the views
from django.http import HttpResponse
import datetime

def hours_ahead(request, offset):
    try:
        offset = int(offset)
    except ValueError:
        raise Http404()
    dt = datetime.datetime.now() + datetime.timedelta(hours=offset)
    html = "<html><body>In %s hour(s), it will be %s.</body></html>" % (offset, dt)
    return HttpResponse(html)

When I try to enter the address
site//time/plus/3/ in the browser, the error Page not found (404) is displayed, why?
here is the whole error:
spoiler
Page not found (404)
Request Method: GET
Request URL: 127.0.0.1:8000/time/plus/1
Using the URLconf defined in first.urls, Django tried these URL patterns, in this order:
^admin/
^time/plus(\d+)/$
The current path, time/plus/1/, didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
aRegius, 2017-07-13
@BotaniQ_Q

1. \d+
2. (\d+)
3. This is an example from a 2009 book (Django 1.1)... Do you need it? :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question