Starting with Django
This guide is going to help you start developing your Django applications at SourceLair and make the most out of SourceLair's capabilities.
This guide assumes that you already have
You can install dependencies to your Django project in 2 ways:
- Terminal: Open your terminal, type and run
pip install -r requirements.txtto install your dependencies from a requirements.txt file or
pip install <package_name>to install a specific package - e.g.
pip install djangorestframework.
- Command Palette: Open Command Palette, type
pipand run the
Pip: Install pip dependenciescommand that will install dependencies according to your
Setup your server
SourceLair provides all Django projects with a dedicated, publicly accessible web server to run and view your Django web applications. Starting a new Django project works out of the box thus you don't have to spend time on it.
Django server command
By default, your Django server runs the
./manage.py runserver 0.0.0.0:8000 command. In case you want to use any other custom command you can read how to do it at the Customize your SourceLair server help page.
Cloning a Django app
If you clone a Django app at SourceLair, don't forget to keep your
manage.py file in your root directory. In case you have a different structure please read our help page in order to customise your
Preview your application
SourceLair provides you with a public URL in order to help you test and showcase your work. There are 2 ways to see the public URL of your Django project:
- Click on the eye icon in the sidebar
- Open the Command Palette and use the
Server: Open Public URLcommand.
SourceLair provides you with intelligent as-you-type autocomplete for your Python files. Completion suggestions appear automatically whenever you are editing a python file. The suggestions include python built-ins, results from your project's static analysis as well as libraries you have installed. The continuous suggestions on keystroke are enabled by default.
To disable the as-you-type autocomplete open up your Control Panel, navigate to your Editor Settings and uncheck the Show suggestions on keystroke option. To manually trigger autocompletion afterwards use Ctrl + Space when you need them.
Our editor will automatically check your Python file for syntax errors when you save it and display all errors and warnings in the left side of the editor, next to the line number. This way you can produce better and safer code, since you do not have to run your application before correcting your mistakes.
If you find this feature too distracting for you, just navigate to your Editor Settings and switch the Python linting option off.
The recommended way to deploy your Django application to production is using Heroku. SourceLair's integration with the Heroku toolbelt makes things pretty simple as you can deploy your app without even leaving SourceLair.
Before you start setting up your deployment workflow, check out that you have:
Django settings for static assets
This step is required only if you want to deploy the default SourceLair Django project. In case you have handled your static assets you can go to the Install and setup Gunicorn for production part of this section.
Add the following settings to your
settings.py in order handle your static assets:
Then you need to create a
staticfolder inside your project's directory (in the same level with your
settings.py). It is the target directory that
collectstatic uses and you need to deploy your project. Git does not support empty file directories, so you will have to create a file inside that directory as well.
my_django_website/ static/ randomfile __init__.py settings.py urls.py wsgi.py manage.py
Django does not support serving static files in production. That's why we are going to use the WhiteNoise project.
First, install WhiteNoise with pip, and add it to your requirements.txt file, running in your terminal:
$ pip install whitenoise $ pip freeze | grep whitenoise >> requirements.txt
Next, install WhiteNoise into your Django application. This is done in
from django.core.wsgi import get_wsgi_application from whitenoise.django import DjangoWhiteNoise application = get_wsgi_application() application = DjangoWhiteNoise(application)
Install and setup Gunicorn for production
While developing your application, you should run the Django application server with the
manage.py runserver command - you can find this inside the
When deploying your application for production load, you need an application server that can handle load better, like Gunicorn. That's why you need to create a
Procfile file that will give the appropriate instructions to Heroku about your server commands in production. So run in your terminal:
$ echo "web: gunicorn server:app --log-file -" > Procfile
In case that Gunicorn isn't installed in your project, run in your terminal:
$ pip install gunicorn $ pip freeze | grep gunicorn >> requirements.txt
Set up Heroku
SourceLair terminal is integrated with Heroku toolbelt thus you are able to use all Heroku commands via SourceLair. Initially, you need to login to Heroku filling in your Heroku credentials
$ heroku login
Then, you need to create a new Heroku application that you can push to.
$ heroku create
Don't forget to commit your last changes before deployment.
Everything should be set up, so you need deploy your app from SourceLair to Heroku. To do so use the Command Palette command
Now every time you want to push something new to production, all you have to do is commit your changes and deploy to Heroku.
In case you use specific port in your Django app (e.g. 8000), don't forget to bind the server of your Django project to the
$PORTenvironment variable, to make sure it works on Heroku.
Tips to code faster
SourceLair provides you with some great features in order to help you develop faster your Django app being focused on the creative part of programming.
Go to definition
When searching for the definition of a class or method, you can just press Alt + . or Alt+ Click on it in order to go to its definition. Please note that when requesting the definition of an external library, goto definition will work as goto assignment instead.
Read the documentation of a model or a class, focusing your cursor on it and using the Ctrl + I shortcut.
Command Palette is a free text search tool which helps you use most of SourceLair's features really fast. You can open it by pressing Ctrl/Cmd + Shift + P.
Quickopen is a fast and time saving way to navigate through your filesystem in order to find and open any file you want using your keyboard.
There are 2 ways to open the Quickopen prompt:
- using its shortcut Ctrl/Cmd + Shift + O
Quickopen: Open a filecommand from Command Palette (Ctrl/Cmd + Shift + P)
Find in files
Find in files is a simple and easy way to search for something in your entire project at once, with quick file navigation from the results. There are 2 ways to trigger this feature:
Project:Find in files command from Command Palette
using its shortcut Ctrl/Cmd + Shift + F