'django urls: "Django tried these URL patterns"

I am trying a tutorial on Django called blog. I have the following structure:

FirstBlog|FirstBlog
settings
urls
__init__
etc

blog
templates | index.html
migrations
views.py

manage.py

The view.py has

from django.shortcuts import render
from django.shortcuts import render_to_response
from blog.models import posts

def home(request):
    return render('index.html')

The urls.py has

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

urlpatterns = [
    url(r'^blog', 'FirstBlog.blog.views.home',name='home'),
]

and I get this error:

Using the URLconf defined in FirstBlog.urls, Django tried these URL patterns, in this order: ^blog [name='home']

The current URL, , didn't match any of these.

I can't seem to get it right.. Any help would be appreciated.

Thank you,



Solution 1:[1]

You are requesting for / url and you have not saved any such mapping. Current mapping is for /blog . So it will work for the same url. i.e goto the browser and request /blog

If you need it to work for / then change the urls appropriately.

Solution 2:[2]

within your blog app, create a urls.py file and add the following code which calls the home view.

blog/urls.py

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

urlpatterns = [
    #url(r'^',views.home, name='home'),

]

then in your root urls file which can be found at FirstBlog/urls.py

from django.conf.urls import url, include

from django.contrib import admin

urlpatterns = [
    url(r'^blog/',include('blog.urls')), #when you visit this url, it will go into the blog application and visit the internal urls in the app
]

PS: your templates should be in blog/templates/blog/index.html

Read this docs on templates to understand how django locates templates.

This one is to understand how urls work Urls dispatcher

Solution 3:[3]

You are doing this in the wrong way! Try doing that using TemplateView from class-based views, which are the current standard from django views.

Check the django documentation: https://docs.djangoproject.com/en/1.9/topics/class-based-views/

Use this at the urls.py:

from django.conf.urls import url
from django.views.generic import TemplateView

urlpatterns = [
    url(r'^blog/', TemplateView.as_view(template_name="index.html")),
]

And then, create a folder called templates in the project root ( like this: https://docs.djangoproject.com/en/1.9/intro/tutorial03/#write-views-that-actually-do-something ) named index.html

Solution 4:[4]

Simply go to file then select Save all your project instead of save. Or use shortcut Ctrl +k s on windows. Project should be able to sync and display the code on Django interface

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 iamkhush
Solution 2 Bernard 'Beta Berlin' Parah
Solution 3 silviomoreto
Solution 4 Mandy