Asif Mushtaq
Asif Mushtaq

Reputation: 3798

How to import urls from apps in django main urls file

I have the following project structure.

Django Project Structure

This is my INSTALLED_APPS array.

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'climemanage.clients'
]

Then I tried to add the clients.urls file in the parent urls file as the below.

from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('admin/', admin.site.urls),
    path('clients/', include('clients.urls'))
]

But I'm getting this error. ModuleNotFoundError: No module named 'clients'

I can solve this by using the following path. include('climemanage.clients.urls') but I want to skip climemanage from the path.

I have tried different ways as,

import clients
import climemanage.clients
from climemanage import clients

But nothing is working.

Upvotes: 3

Views: 2727

Answers (2)

ha-neul
ha-neul

Reputation: 3248

Your project structure is wrong. It seems either startproject part or startapp part might gone wrong. It might be best if you could list out how you got up to this point. Please see the structure I put at the end of my answer.

In your case, your main settings.py is under clients. This means that clients is the result of startproject. Currently you do not have an app, but you have a bunch of files that should be appeared with app that now under migrations folder.

Beause of that, you will never be able to add anything from clients.urls to main urls.py. Because your clients is the place where your main urls.py located.

I put what I normally do here (this mac version, if you are using windows, you need check django tutorial)

$ mkdir clienmanage
$ cd clienmanage
$ python3 -m venv myvenv      # set up virtual environment
$ source myvenv/bin/activate  # go into virtual envionment
After this step you can install django

then after that, when you run your startproject command, make sure run like below:

django-admin startproject project .

Note the . at the end.

After that, you should have structure like:

clienmanage
|--- manage.py
|--- project
|   |--- __init__.py
|   |--- settings.py
|   |--- urls.py
|   |--- wsgi.py
|--- virtual
|
|___ requirements.txt

then you will:

$ python manage.py startapp clients

clienmanage
|
|---clients # this is the app
|    |---migrations
|    |---models.py
|    |---views.py
|    |---apps.py  
|--- manage.py
|--- project
|   |--- __init__.py
|   |--- settings.py
|   |--- urls.py
|   |--- wsgi.py
|--- virtual # a folder
|
|___ requirements.txt

After this step, django will not automatically generate app level urls.py and forms.py. You should generate these files inside the app folder (in my example case, clients)

****If you want to put app in a different directory You can specify destination.

$ python manage.py startapp <app_label> [destination] 

in the above case, if you want to have your app inside of project

$ python manage.py startapp clients project/clients

If you took this route, make sure to add project.clients rather than clients in your INSTALLED_APPS.

Upvotes: 2

Ashwathama
Ashwathama

Reputation: 86

In main urls.py file you can add the following code snippet.

from django.urls import path, include 

urlpatterns = [
    path('admin/', admin.site.urls),
    path('something/', include('clients.urls')),
]

In settings.py file

INSTALLED_APPS = [
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'rest_framework',
 'webapi',  # like in my case my appname was webapi
 'nameofyourapp', #pay attention here
]

Upvotes: 1

Related Questions