Django urls help - unable to access view

Let me start by saying that I am new to Django, and I understand that this seems easy (and I hope so).

I am following a tutorial that takes me through the basic setup of a simple Django site, and I found myself stuck at the point where I just call for a new look, I just added in the tutorial (for those who are interested, the tutorial is here https: / /overiq.com/django/1.10/views-and-urlconfs-in-django/ )

So, my server is working for me, and I'm trying to access

http://127.0.0.1:8000/blog/time/

When I try to access this view, I get an error message:

Using the URLconf defined in mysite.urls, Django tried to use this pattern URL in this order:

^blog ^time/$ [name='todays_time']
^blog ^$ [name='blog_index']
^admin/

The current path, blog / time /, does not match any of them.

Here is the layout of my Django project:

Django Project Design

My mysite urls.py file looks like this:

from django.conf.urls import url, include
from django.contrib import admin

urlpatterns = [
    url(r'^blog/', include('blog.urls')),
    url(r'^admin/', admin.site.urls),
]

My blog package urls.py file is as follows:

from django.conf.urls import url
from blog import views

urlpatterns = [
    url(r'^time/$', views.today_is, name='todays_time'),
    url(r'^$', views.index, name='blog_index'),
]

my blog package views.py looks like this:

from django.http import HttpResponse
import datetime

def index(request):
    return HttpResponse("Hello Django!!!")

def today_is(request):
    now = datetime.datetime.now()
    html = '''
            <html>
                <body>
                    Current date and time: {0}
                </body>
            </html>
            '''.format(now)
    return HttpResponse(html)

I can successfully switch to

http://127.0.0.1:8000/blog

and see the response from index () as "Hello Django !!!" in my browser. So I expected

http://127.0.0.1:8000/blog/time/

to work fine, but it’s not, and trying to see this is what gives the error I shared above.

Can someone point me in the right direction? I will be grateful! Thank you all!

. , , , !

+4
2

, @LeLouch:

url(r'time/', views.today_is, name='todays_time')

^? . :

^blog ^time/$ [name='todays_time']

^ , . blog.urls . , ^.

, ^blog/time/$. ^blog ^$ [name='blog_index'].

+1

//, URL- - /^

urlpatterns = [
    #you have a carrot in here
    url(r'^time/$', views.today_is, name='todays_time'), 
    #it should be 
    url(r'time/', views.today_is, name='todays_time'),

]

URL

+1

Source: https://habr.com/ru/post/1692950/


All Articles