Project

General

Profile

Django

Helpful information for working with Django.

Pydoc

Pydoc is the Python documentation generator.

Pydoc for Django Fix

A solution was derived from this page: https://stackoverflow.com/questions/24772805/python-documentation-for-django-views-generic-with-pydoc by changing from cli mode to browser mode.

Create this as a file called my_pydoc.py in the same folder as your manage.py file for the project. Change "iot_rpi" to the name of your Django site:

import django
import pydoc
import os

os.environ['DJANGO_SETTINGS_MODULE'] = 'iot_rpi.settings'
django.setup()
#pydoc.cli()
pydoc.browse(9999)

Now, from within your Python virtual environment (remember to use the "source" command!) run this to start your web browser with the documentation:

python -m my_pydoc
or
python my_pydoc.py

You should now be able to access the documentation for your Django project and it's apps - as well as documentation for Python itself.

Pydoc for Python 3.x Programs (2024 - Obsolete!)

Pydoc is actually a script that ships with Python. Unfortunately as of 2022 and Ubuntu 20.04, it is hard coded to use Python 2.7 and thus fails when attempting to work with 3.x programs.

The fix is to create you own command so that it supersedes the provided one. This will allow it to work seamlessly for all usage, whether 2.7 or 3.x and will work with Python virtual environments

Your replacement pydoc must be in a folder on your path that precedes the standard /usr/bin operating system folder. Copy the shared pydoc to this location. On Ubuntu 20.04 this would be

cp /usr/bin/pydoc ~/.local/bin/pydoc

Then edit the first line. Currently this is

#!/usr/bin/python2.7

Replace that with

#!/usr/bin/env python

By using the env command, the shell will automatically determine the current version of Python in use and run the pydoc module with that.

Now the script must be made executable

chmod +x ~/.local/bin/pydoc

Now when you run pydoc it will use your copy not the shared one and will run the appropriate version of Python that you are using.

Apache Integration

If you run into issues integrating your Django site to be served from Apache for production, this information may help. It is not fully vetted, so some experimentation may be required. This example assumes the Django project is called ADC_server.

# Apache server requires read and write access to these
chmod a+w ~/Documents/django/project/ADC_Server/db.sqlite3
chmod -R a+w ~/Documents/django/project/ADC_Server/sent_emails/
sudo chgrp -R www-data /home/capstone
chmod g+rwxs ~/Documents/django/project/ADC_Server/

# add capstone to the adm group so you can see the Apache logs for troubleshooting
sudo usermod -aG adm capstone

Pylint

Per their site, "Pylint is a source-code, bug and quality checker for the Python programming language".

Pylint has a command line argument to support Django! This is mentioned on this page: https://pylint.readthedocs.io/en/latest/user_guide/ide-integration.html