[Fixed]-Django URL and Views

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

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

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

Leave a comment