0

Since this below method of Mapping Django Url from a view is depreciated in Django 1.9 and above

url(r'^contact/$', 'newsletter.views.contact', name='contact'),

url(r'^about/$', 'home.views.about', name='about'),

And this is what is currently in place

from newsletter import views'

url(r'^about/$', 'views.about', name='about'),

How do i Map my url for 2 different app views

from home import views

from newsletter import views

url(r'^home/$', 'views.home', name='home'), #located in home

url(r'^about/$', 'views.about', name='about'), #located in newsletter

Mapping like i did above will result to an error so i need help. New to Django

Phexcom
  • 5
  • 4

3 Answers3

1

I don't understand what you mean by using a fully qualified module name being deprecated since it is a core python construct. But you can manage two different modules containing submodules with the same name by binding them to different aliases using the "import as" statement.

Example:

from home import views as home_view
from newsletter import views as news_view

Then you can use the aliases home_view and news_view to reference each module instead of views, throughout the declared namespace.

You can take a look at the import statement syntax in the Python docs here:

If the requested module is retrieved successfully, it will be made available in the local namespace in one of three ways:

  • If the module name is followed by as, then the name following as is bound directly to the imported module.
  • If no other name is specified, and the module being imported is a top level module, the module’s name is bound in the local namespace as a reference to the imported module
  • If the module being imported is not a top level module, then the name of the top level package that contains the module is bound in the local namespace as a reference to the top level package. The imported module must be accessed using its full qualified name rather than directly
haey3
  • 56
  • 5
0

Try:

from home import views as home_views

from newsletter import views

url(r'^home/$', 'home_views.home', name='home'), #located in home

url(r'^about/$', 'views.about', name='about'), #located in newsletter
Windsooon
  • 6,864
  • 4
  • 31
  • 50
0

As an alternative you can only import the view functions:

from home.views import home
from newsletter.views import about

urlpatterns = [
    url(r'^home/$', home, name='home'),
    url(r'^about/$', about, name='about'),
]
knbk
  • 52,111
  • 9
  • 124
  • 122