I try to learn Django (1.10.1) and use the recording from the webinar, but got stuck at a moment where I can’t connect a view:

There are 2 folders in the project: MyBlog and blog . MyBlog\urls.py contains:

 from django.conf.urls import url, include from django.contrib import admin urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^', include('blog.urls')) ] 

blog\urls.py contains:

 from django.conf.urls import url, include from django.contrib import admin from . import views urlpatterns = ('', url(r'^$', blog.views.home, name='home'), ) 

blog\views.py contains:

 from django.http import HttpResponse from django.shortcuts import render def home(request): return HttpResponse('Hello, World!') 

I'm confused, help. I understand that there is an error in the blog\urls.py in the line:

 urlpatterns = ('', url(r'^$', blog.views.home, name='home'), ) 

When you run the code issues:

 ... File "G:\Proktor\Projects\MyBlog\blog\urls.py", line 23, in <module> url(r'^$', blog.views.home, name='home'), NameError: name 'blog' is not defined 
  • And what error gives? Attach the text of the error. In general, you should also have views.home , views.home ? - Flowneee
  • one
    All right Is that only could be done like this: urlpatterns = (url(r'^$', views.home, name='home'), ) - Mr. Fix
  • @Flowneee added error code. If you replace blog.views.home with views.home you views.home the error: File "C:\Python27\lib\site-packages\django\core\management\base.py", line 420, in check raise SystemCheckError(msg) django.core.management.base.SystemCheckError: SystemCheckError: System check identified some issues:ERRORS: ?: (urls.E004) Your URL pattern '' is invalid. Ensure that urlpatterns is a list of url() instances. HINT: Try removing the string ''. The list of urlpatterns should not have a prefix string as the first element. System check identified 1 issue (0 silenced). File "C:\Python27\lib\site-packages\django\core\management\base.py", line 420, in check raise SystemCheckError(msg) django.core.management.base.SystemCheckError: SystemCheckError: System check identified some issues:ERRORS: ?: (urls.E004) Your URL pattern '' is invalid. Ensure that urlpatterns is a list of url() instances. HINT: Try removing the string ''. The list of urlpatterns should not have a prefix string as the first element. System check identified 1 issue (0 silenced). - EmptyMan
  • @Mrfix your offer helped me! Thank. Write the answer and I will mark it as a solution to my question. (but didn’t understand why it worked. And why was it offered on the webinar’s recording exactly as it was recorded, that is, not the working version?) - EmptyMan
  • @EmptyMan, please :) - Mr Fix

1 answer 1

All right Is that only could be done like this:

 from django.conf.urls import url from . import views urlpatterns = ( url(r'^$', views.home, name='home'), )