Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - include app urls

I have the following structure (Django 1.4):

containing_dir/
    myproject/
        myapp1/
        myapp2/
        myapp3/

myproject, myapp1, myapp2, and myapp3 all have init.py, so they're all modules.

In manage.py (under containing_dir) I have os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")

in myproject.settings i define:

[..]
ROOT_URLCONF = 'myproject.urls'
INSTALLED_APPS = (   
    [..]
    'myproject.myapp1',
    'myproject.myapp2',
    'myproject.myapp3',
)
[..]

In myapp1.urls.py I define:

urlpatterns = patterns('myapp1',
    url(r'^agent/$', 'views.agent',    name='agent')
)

and I try to import it in myproject.urls I try to import myapp1 urls like this:

(r'^myapp1/', include('myproject.myapp1.urls'))

but whenever I try lo load localhost:8000/myapp1/agent I get

Exception Value: No module named myapp1

I think thrown from withing myapp1.urls

Any help? thanks

like image 342
pistacchio Avatar asked Apr 08 '13 17:04

pistacchio


People also ask

What is include in Django URL?

include() also accepts as an argument either an iterable that returns URL patterns or a 2-tuple containing such iterable plus the names of the application namespaces. Parameters: module – URLconf module (or module name) namespace (str) – Instance namespace for the URL entries being included.

How can we handle URLs in Django?

Django provides tools for performing URL reversing that match the different layers where URLs are needed: In templates: Using the url template tag. In Python code: Using the reverse() function. In higher level code related to handling of URLs of Django model instances: The get_absolute_url() method.

What is dynamic URL in Django?

Being able to capture one or more values from a given URL during an HTTP request is an important feature Django offers developers. We already saw a little bit about how Django routing works, but those examples used hard-coded URL patterns. While this does work, it does not scale.


2 Answers

In myproject.settings make following changes :

INSTALLED_APPS = (   
[..]
'myapp1',
'myapp2',
'myapp3',
)
like image 51
Tejas Avatar answered Oct 14 '22 17:10

Tejas


You must have a

__init__.py

file inside your "myproject" directory. When you say:

(r'^myapp1/', include('myproject.myapp1.urls'))

you are saying "myproject" (as well as myapp1) is a python packege.

like image 7
Raydel Miranda Avatar answered Oct 14 '22 17:10

Raydel Miranda