Deploy your Django/REST API in Python Anywhere (Free)

Redwan Ali Rafi
5 min readMay 24, 2024

--

As developers, we make projects, but we must deploy them online to make them live. There are a few online services that allow you to make your projects live. I used to use Vercel, but recently, for some issues, I was looking for alternatives, and I found Python Anywhere.

Complete steps to successfully deploy your Django/REST project:

Step-1:

Create a free account on pythonanywhere.com, then go to bash.

Step-2:

To deploy my project, I’m going to upload the project zip file. If your project is on Git Hub, you can use commands like git clone [repository-SSH/HTTPS].

To upload the zip, go to files, click the upload button, and select your zip.

Step-3:

Now, it's time to unzip your zip file. Go to bash (terminal) and write the following command:

upzip filename.zip

Ok, now, in the file manager, you can see your project folder. It’s time to configure it and make it live.

Step-4:

Let’s create a virtual environment and import all the libraries.

Navigate to your project directory (My project folder is called Nirvar) and write the following commands.

cd Nirvar
python -m venv virtualenv
source vitualenv/bin/activate

or

virtualenv venv
source venv/bin/activate

Now that the virtual environment is set let's download the libraries. If your project has requerements.txt, then you can simply use:

pip install -r requirements.txt

Or manually install it like this:

pip install django mysqlclient markdown djangorestframework djoser djangorestframework-simplejwt drf-nested-routers pillow

Step-5:

Now, go to the web tab to create a web application. Click on Create a New app, then select Manual Config, then select Python version (My case is the latest one).

Step-6:

Go to files and navigate to your app settings.py. Inside settings.py, you will see an Allowed Host option. If you want a specific host to access this, then you can add that host. I want to give access to everyone. So:

ALLOWED_HOSTS = ["*"]

Now, Let’s collect some paths:

Main Project Path: /home/Tqsko1/Nirvar/

( Nirvar is the project folder in the file manager )

Project Folder Name: nirvar (Where your app settings.py exists)

Virtual env Path: /home/username/nirvar/virtualenv

Step-7:

Now, let's configure the paths that we collected. Go to the web section. First, put the main project path in the source code section:

Now, put the Venv path in the virtualenv section:

Now, click on the WSGI configuration file in the Code section. In the wsgi.py file, remove everything except the Django portion and uncomment it.

Change these two variables accordingly, and now the wsgi.py should look like this:

Ok, We’re almost done. All we need is to configure the static and media files. Remember, even if you don't use the static files, you need to configure it unless the default rest/Django CSS won't load.

Step-8:

Configuration of static files:

In settings.py, make sure you have these two lines. If you don't have STATIC_ROOT, then add it.

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

In the terminal, write this to collect the static files of Django.

python manage.py collectstatic

Go to the web section and navigate to the static file section. Add the following:

  • URL: /static/
  • Directory: /home/Tqsko1/Nirvar/staticfiles

here Tqsko1is my username, and Nirvar is my project folder name.

Now it should look this:

Step-9:

Configuration of media files:

In settings.py, make sure you have these two lines.

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

In your main app urls.py, make sure you have this:

If Debug in settings.py is True:

urlpatterns = [
path('admin/', admin.site.urls),
#other paths
]

if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

If Debug in settings.py is False:

urlpatterns = [
path('admin/', admin.site.urls),
#other paths
]+static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Now go again into the static file section and add the following, the same as you did in the static file section.
Add a new entry:

  • URL: /media/
  • Directory: /home/yourusername/yourproject/media (replace yourusername and yourproject with your actual username and project name)

It should look like this:

Now, you need to verify the permission. Your uploaded images won't save if you don't do this. Go to the terminal and write the following command.

$ chmod 755 /home/yourusername/yourproject/media

All set! WAIT…

Make sure you migrated everything. Your database won't work if you don't. Go to the terminal and write them to migrate:

python manage.py makemigrations
python manage.py migrate

Now go to the web section and click Reload. Now go to your domain, and hopefully, everything will be working fine.

Congratulations! Your project is LIVE

This is my very first post on this platform. I hope this was helpful. Do comment if you face any problems. Thank You.

--

--