Howard Lee
Howard Lee

Reputation: 987

Django - how to wire urls correctly? url(), include(), ImportError

I try to structure my project by putting applications under an "apps" folder, like so:

├── manage.py
├── mysite
│   ├── apps
│   │   ├── __init__.py
│   │   ├── myapp1
│   │   │   ├── __init__.py
│   │   │   ├── models.py
│   │   │   ├── urls.py
│   │   │   └── views.py
│   │   └── myapp2
│   │       ├── __init__.py
│   │       ├── models.py
│   │       ├── urls.py
│   │       └── views.py
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   ├── wsgi.py

And in mysite/urls.py:

from django.conf.urls import patterns, include, url

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^myapp1/', include('mysite.apps.myapp1.urls')),
    url(r'^mysite/apps/myapp2/', include('myapp2.urls')),
    url(r'^admin/', include(admin.site.urls)),
)

There is something wrong with:

    url(r'^myapp1/', include('mysite.apps.myapp1.urls')),
    url(r'^mysite/apps/myapp2/', include('myapp2.urls')),

I could not wire either myapp1 or myapp2 correctly, Django gives me "ImportError...no module named myapp1..." Any help?

Upvotes: 2

Views: 558

Answers (2)

liuzhijun
liuzhijun

Reputation: 4469

maybe like this:

include('mysite.apps.myapp1.urls')),

update

you can try:

add a file __init__.py in the mysite dir

Upvotes: 0

Izkata
Izkata

Reputation: 9323

You're missing a level in the relative path:

url(r'^mysite/apps/myapp2/', include('apps.myapp2.urls')),

myapp1 looks like it should work to me.


A note, comparing how you're trying to include myapp1 vs myapp2, it looks like you may have misunderstood the structure slightly. The URL has nothing to do with the code layout. This is completely valid:

url(r'^zimzam/allthethings/', include('apps.myapp2.urls')),

Upvotes: 1

Related Questions