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 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
- After that run
pip3 show django-friendshipin 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.
- Click PyCharm —> Preferences menu item.
- Click the Project Interpreter menu item in the left panel.
- Then click the button
+to open the package installation popup window, the window title is Available Packages.
- 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.
- 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.
- 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', ]
- 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.
- Open a terminal and cd into your local Django project root folder.
- Run the command
python3 manage.py migrate friendshipin 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
- 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/ )
- 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 the 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 the django-friendship GitHub website for detailed usage.