Create your new Django app

How to create a new Django app using Imagine, and useful Imagine commands

Install

If this is your first time using Imagine, you will need to install the Imagine SmartCompiler on your computer. (Note: If you have previously installed Imagine, your can skip to the section on how to create a new Django app with Imagine).


Install npm

  • To be able to install the Imagine SmartCompiler, you need to have the npm package manager installed on your machine.
  • To check if you have npm installed you can run this command in your terminal:
npm -v
  • If you get the error npm: command not found when you run the command, you will need to install npm first.

Install Imagine

  • Once you have npm installed, run the following command to install Imagine on your computer:
npm install -g imagine
  • You can check that the compilation was successful by running the following command in your terminal:
imagine -h
  • You should see the following output in your terminal:
Header: Imagine compiler
Usage: imagine [-v|--version] COMMAND
ProgDesc: Imagine compiler
Available options:
-h,--help Show this help text
-v,--version Show version
Available commands:
create
compile
  • if you get the error imagine: command not found, it points to an issue with your npm installation or environment settings. If this happens, please talk to us and we'll troubleshoot this live with you.

Create

  • Creating a Django app has never been easier than with Imagine!

  • Once you have installed Imagine on your computer, mkdir a new directory from the command line - this would be where you want to store the source code for your new app.

  • Next, cd into your new directory.

  • Then run the following command (you can replace myapp with your chosen name for your new app):

imagine create -f django -n myapp
  • The above command will setup a new basic django app in the current directory - you can ls to see the new generated files:
.
โ”œโ”€โ”€ Makefile
โ”œโ”€โ”€ PipFile
โ”œโ”€โ”€ manage.py
โ”œโ”€โ”€ musicapp
โ”‚ย ย  โ”œโ”€โ”€ __init__.py
โ”‚ย ย  โ”œโ”€โ”€ admin.py
โ”‚ย ย  โ”œโ”€โ”€ apps.py
โ”‚ย ย  โ”œโ”€โ”€ models.py
โ”‚ย ย  โ”œโ”€โ”€ tests.py
โ”‚ย ย  โ””โ”€โ”€ views.py
โ”œโ”€โ”€ myapp.im
โ””โ”€โ”€ project
โ”œโ”€โ”€ __init__.py
โ”œโ”€โ”€ asgi.py
โ”œโ”€โ”€ settings.py
โ”œโ”€โ”€ urls.py
โ””โ”€โ”€ wsgi.py

Run

  • You can run your new app by running the command below. This command allows us run your app, including installing any packages that are needed.
make install && make run
  • Once the command has finished executing, you will see the following message:
...
System check identified no issues (0 silenced).
January 13, 2021 - 04:25:08
Django version 3.1.5, using settings 'project.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
  • You can now open the http://127.0.0.1:8000/ url, you will see a page that shows the install worked successfully.
yay
You should see this on success
  • Congrats! At this point you have your new Django app running!

Add functionality

  • You can now begin to add your application features by making updates to the .im config file : myapp.im

  • You can start by reading more here about how you can add functionality to your app (TL;DR you can select your app settings, and represent various app domains, such as data models, APIs, storage and Authorization using Imagine's simple intutive syntax, in your myapp.im file).

  • you can see an example of what an Imagine config file and syntax looks like, as well as the various files generated on our home page (remember to click the hamburger menu on the top of the right codebox to see the full list of generated folders and files).

  • You can also check out out our demo app, which includes an example functioning Imagine demo.im config file and all the output files that are generated from this single file.


Compile

  • Once you add functional code domains or change settings in your .im config file, you can use the Imagine SmartCompiler to automatically generate Django code that implements the new functionality you added in the .im file by running the following command in your terminal:
imagine compile myapp.im
  • Once your compile command finishes, you can run the updated app with the new functionality by running the command below:
make install && make run
  • Once the command has finished executing, you will see the following message:
...
System check identified no issues (0 silenced).
January 13, 2021 - 04:25:08
Django version 3.1.5, using settings 'project.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
  • If you now go to the http://127.0.0.1:8000/ url you will be able to see and use the new functionality you added. For example, if you added some new APIs, they will be listed here.

  • You can also see the generated source code files and the implemented functionality by opening the app directory in your code editor.


Test

  • In addition to generating code for your functionality, the Imagine SmartCompiler also generates an extensive suite of tests to make sure your app is working as you expect it to.

  • You can run the SmartCompiler's auto-generated tests by by running the command below:

make coverage
  • This command will run the suite of generated tests and print the current test coverage percentage. You can also see a more detailed report by opening the htmlcov/index.html file in your current directory in a browser.

  • You can view the source code for the generated tests by opening the myapp/tests directory in your code editor.


Deploy

  • The compile command also generates docker configuration files that allow you to package the app in a container that you can then deploy to any cloud.

  • You can create a docker build image by running the following command:

docker-compose build
  • Once the command finishes, you can check the generated docker builds by running the command:
docker images
  • You will see the following output in your terminal:
REPOSITORY TAG IMAGE ID CREATED SIZE
myapp_nginx latest e6a7247e3ac8 2 seconds ago 21.3MB
myapp_web latest 5d8cf598d724 4 minutes ago 129MB
  • You can start these docker images on your computer by running the following command:
docker-compose up
  • You can now follow instructions from your cloud provider on how to deploy a Docker build image to production.