-1

I am struggling with urls.py.

Error:

[pylint] E0602:Undefined variable 'patterns'

In code:

from django.conf.urls import *
from django.contrib import admin

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'mysite.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),
    url(r'^$', 'notes.views.home', name='home'),
    url(r'^admin/', include(admin.site.urls)),
)

I am following the tutorial on link : Django Tutorial: Building a note taking app

Problem number 2.

Same problem in tutorial : Simple Django Web Application Tutorial

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

urlpatterns = [
    url(r'\^admin/', admin.site.urls),
    url(r'\^myrestaurants/', include('myrestaurants.urls', namespace='myrestaurants')),
]
cezar
  • 11,616
  • 6
  • 48
  • 84
PyDeveloper
  • 309
  • 6
  • 24

2 Answers2

1

This is deprecated from 1.10:

urlpatterns = patterns('')

So make sure you're using a compatible version of Django.

cezar
  • 11,616
  • 6
  • 48
  • 84
DariusFontaine
  • 682
  • 1
  • 4
  • 20
1

You are following a tutorial for Django 1.7, but using yourself Django 2.0.7. Django 2 isn't backward compatible with older versions and this can lead to pitfalls.

I'd strongly recommend you to follow a tutorial for the current version. The official documentation has a good tutorial.

Unless you are tasked to maintain a legacy Django project, you don't need to know how things have been done in past versions. Start with the current version, which is now Django 2, and learn how the things have to be done. This is my opinion and might eventually colide with the beliefs of other developers.

The problem you're facing can be resolved with some adaptions. First of all, the line:

from django.conf.urls import *

is a bad practice, regardless of the Django or Python version you're using.

Import the modules you need explicitly. Django 2 has a slightly different approach when it comes to routes. Although you could still use url, the new way is to use path. The urlpatterns should be a list containing path objects.

The proper import should be:

from django.urls import path, include

Your urlpatterns should look like:

urlpatterns = [
    path('', 'notes.views.home', name='home'),
    path('admin/', include(admin.site.urls)),
]

Even better approach would be to define urls.py in your apps and include it in the main urls.py in your project directory:

urlpatterns = [
    path('myrestaurants/', include('myrestaurants.urls', namespace='mysrestaurants'),
    path('admin/', include(admin.site.urls)),
]
cezar
  • 11,616
  • 6
  • 48
  • 84