Making your Django Apps reusable for other Projects

Making your Django Apps reusable for other Projects

Should in case you want to re-use your django app in other future projects instead of rewriting the entire project, we can package the other app and install it in our django project.

.> Create a new folder and move your django-application to the folder. example. django-store
.> Open the folder you just created in your system command prompt

.> Update python setuptools

pip install --upgrade setuptools

Setuptools is a package development process library designed to facilitate packaging Python projects by enhancing the Python standard library distutils

.> Create a new file called README.rst in the folder you created that contains your django-application

this's where you'll describe your application. example:

This's my django application called josephscrumy

Quick Start

add "josephscrumy" in your INSTALLED_APP like this:

    INSTALLED_APP = [
        ---
        'josephscrumy',
    ]

application url is should be included in your project urls.py like this: path('josephscrumy/', include('josephscrumy.urls'))

then run "python manage.py migrate". to migrate your database

run your server

and visit this link 127.0.0.1:8000/josephscrumy (to see the application homepage)

.> Create a new file called setup.py in the same folder structure. This file will contain settings for our setuptools.

import os
from setuptools import find_packages, setup

with open(os.path.join(os.path.dirname(__file__), 'README.rst')) as readme:
    README = readme.read()

# Allow setup.py file to be run from any path
os.chdir(os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir)))

setup(
    name = 'django-store',
    version = '0.1',
    packages = find_packages(),
    include_package_data = True,
    license = 'BSD License', # Your License
    discription = 'short description about your app',
    long_description = README,
    url = 'https://www.example.com/',
    author = "Your name",
    author_email = "youremail@gmail.com",
    classifiers = [
        'Environment :: Web Environment',
        'Framework :: Django',
        'Framework :: Django :: X.Y', # replace x,y as appropriate
        'Intended Audience :: Developers',
        'License: OSI Approved :: BSD License', # example license
        'Operating System :: OS Independent',
        'Programming Language :: python',
        'Programming Language :: python :: 3.6',
        'Topic :: Internet :: WWW/HTTP',
        'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
    ],

)

To generate a license. visit this website choosealicense.com

.> Create a new file called LICENSE.txt and paste your license generated.
.> Create a new file called MANIFEST.in

A MANIFEST.in file consists of commands, one per line, instructing setuptools to add or remove some set of files from the sdist.

.> Add this code to your MANIFEST.in file

include LICENCE.txt
include README.rst
recursive-include yourappname/static *
recursive-include yourappname/templates *


Then open this folder in your terminal. the folder we created from the beginning where we've our application folder and, README.txt file.

let's finally build the application package. run this command

python setup.py sdist --format=zip

Notice we specified the format we want. The default format is tar.gz
A new folders will be added to your folder. e.g: dist and django_store.egg-info

Inside the dist folder, that's where you'll find the package that we're going to install and also share with people that wish to use our application

To install the application run this command:

pip install --user django-store/dist/josephscrumy-0.1.zip

if you're using virtual environment, no need to add --user, you can just install it without including --user

pip install django-store/dist/josephscrumy-0.1.zip

To uninstall the application, run this command:

pip uninstall josephscrumy