Mahesh Shitole
Mahesh Shitole

Reputation: 912

Access urlpatterns variable from urls.py in views.py

I am trying to access urlpatterns variable from url.py in view.py. but it gives error

Checkout my following code.

from testproject.urls import *

print urlpatterns

it gives following error.

NameError: name 'urlpattern' is not defined

Can anybody help ?

Upvotes: 1

Views: 164

Answers (1)

ferrangb
ferrangb

Reputation: 2050

It seems that you are trying to print urlpattern instead of urlpatterns. Are you sure this is correct?

To print urlpatterns you have to:

  • From urls.py you should be able to print it directly because urlpatterns is defined here.

  • From views.py you have to import the right urls.py file based on where it is. If you want to import the main urls.py then from project_name.urls import urlpatterns. If you want to import the urls.py of an app then from project_name.app_name.urls import urlpatterns.

NOTE: Remember explicit is always better than implicit, so instead of from testproject.urls import * is better from testproject.urls import urlpatterns

Upvotes: 1

Related Questions