How To Install External Django Application To Django Project Correctly

We all know that one Django project can contain multiple applications, and all the applications should be activated in the Django project settings.py file INSTALLED_APPS section ( please read article How To Create / Remove Django Project Application ), but how to install external Django application developed by others in your local Django project? This article will show you how to do it.

1. Use PIP To Install External Django App To Local Machine.

  1. This example will use the django-friendship library as a demo, so before you install it in your Django project, install it to your local Django libraries first with the pip command as below.
    $ pip3 install django-friendship
    Collecting django-friendship
      Downloading https://files.pythonhosted.org/packages/75/39/03a56d70d2e4cb2da31b2ec1730f066762ed1ec9e1ccd8dea3be475f06bc/django-friendship-1.8.0.tar.gz
    Building wheels for collected packages: django-friendship
      Running setup.py bdist_wheel for django-friendship ... done
      Stored in directory: /Users/zhaosong/Library/Caches/pip/wheels/e1/8d/b7/6421437291a21c44f396a603ed5c74af3b1fa2fc9d76c0b717
    Successfully built django-friendship
    Installing collected packages: django-friendship
    Successfully installed django-friendship-1.8.0
  2. After that run pip3 show django-friendship in terminal to see where the library is installed.
    $ pip3 show django-friendship
    Name: django-friendship
    Version: 1.8.0
    Summary: django-friendship provides an easy extensible interface for following and friendship
    Home-page: https://github.com/revsys/django-friendship/
    Author: Frank Wiles
    Author-email: [email protected]
    License: UNKNOWN
    Location: /Users/zhaosong/anaconda3/lib/python3.6/site-packages
    Requires: 
    Required-by: 
    

2. Install django-friendship Library in PyCharm.

If you use PyCharm to develop the Django application, you can read the article PyCharm Project Add External Library (PyMySQL) Path Example to learn how to add django-friendship library in it. Or you can follow the below steps to add it in PyCharm settings quickly. The below steps are for macOS, If you run PyCharm in Windows or Linux, the steps are similar.

  1. Click PyCharm —> Preferences menu item.
  2. Click the Project Interpreter menu item in the left panel.
  3. Then click the button + to open the package installation popup window, the window title is Available Packages.
  4. Input the keyword django-friend in the search box to search it out and select it in the list, then click the Install Package button on the Available Packages window bottom left corner to install it.
  5. After installing the django-friendship package successfully, you can find the friendship library has been added in the PyCharm project External Libraries —> site-packages folder.

3. Activate django-friendship Library In Django Project.

Now you have installed django-friendship library in all your development environments, then you can activate and use it in your Django project.

  1. Edit Django project settings.py file and add ‘friendship’ in the INSTALLED_APPS section.
    INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'friendship',
    ]
  2. Now start the Django project web server, if you find the below error messages that mean the django-friendship application is not installed correctly.
    ModuleNotFoundError: No module named 'friendship'

4. How To Manage django-friendship Model Objects In Django Project Admin Site.

After you have installed django-friendship library in your Django project successfully,  you also need to migrate it’s models to create related database tables in the backend database server. Then you can manage django-friendship models on the project admin website.

  1. Open a terminal and cd into your local Django project root folder.
  2. Run the command python3 manage.py migrate friendship in the terminal.
    DjangoHelloWorld $ python3 manage.py migrate friendship
    
    Tracking file by folder pattern:  migrations
    System check identified some issues:
    
    WARNINGS:
    ?: (urls.W001) Your URL pattern '^$' uses include with a route ending with a '$'. Remove the dollar from the route to avoid problems including URLs.
    Operations to perform:
      Apply all migrations: friendship
    Running migrations:
      Applying friendship.0001_initial... OK
  3. When above command success, you can operate the FRIENDSHIP model objects in your local Django project admin website ( http://127.0.0.1:8000/admin/ )
    operate-friendship-model-objects-in-django-project-admin-website-
  4. If you use SQLiteStudio to open your Django project sqlite3.db file, you can also see the django-friendship library used tables, all the tables are prefixed with friendship_.

5. How To Use django-friendship In Django Source Code.

  1. Now you can use django-friendship provided API in your Django source code.
  2. First, you should import the below models.
    from django.contrib.auth.models import User
    from friendship.models import Friend, Follow, Block
  3. Then you can use various functions to operate, please see the django-friendship GitHub website for detailed usage.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.