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.
This example will use django-friendship library as a demo, so before you install it in your Django project, install it to your local Django libraries first with 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
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 Django application, you can read article PyCharm Project Add External Library (PyMySQL) Path Example to learn how to add django-friendship library in it. Or you can follow below steps to add it in PyCharm settings quickly. Below steps are for MacOS, If you run PyCharm in Windows or Linux, the steps are similar.
- Click PyCharm —> Preferences menu item.
- Click Project Interpreter menu item in left panel.
- Then click
+
button to open package installation popup window. - Input django-friend in the search box and click Install Package button to install it.
- After install the django-friendship package successfully, you can find the friendship library has been added in PyCharm project External Libraries —> site-packages folder.
3. Activate django-friendship Library In Django Project.
Now you have installed django-friendship in all your development environment, then you can activate and use it in your Django project.
- Edit Django project settings.py file and add ‘friendship’ in INSTALLED_APPS section.
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'friendship', ]
- Now start the Django project web server, if you find below errors that means 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 backend database server. Then you can manage django-friendship models in the project admin website.
- Open a terminal and cd into your local Django project root folder.
- Run command
python3 manage.py migrate friendship
in 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
- When above command success, you can operate friendship model objects in your local Django project admin website.
- 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.
Now you can use django-friendship provided API in your Django source code. First you should import below models.
from django.contrib.auth.models import User from friendship.models import Friend, Follow, Block
Then you can use various functions to operate, please see django-friendship github website for detail usage.