Commands analogue in Django

Hi All!

I am just starting to study the phoenix. Before that, I had a lot of experience with Django.
I know that mix phx.new project is analogous to django-admin startproject project.
Say there is a command similar to django-admin startapp in the phoenix.

Thank!

I do not know anything about django or its tooling, but django-admin startapp sounds like starting the development server for the project. mix phx.server would be the equivalent then.

Most of the tasks available for phoenix are explained at https://hexdocs.pm/phoenix, in the nav bar to the left, click “Mix Tasks”. Similar those for ecto can be found at https://hexdocs.pm/ecto. Each library describes their tasks in their documentation like that. If the “Mix Tasks” section is missing in a libraries doc-page, then it does not have tasks (or documentation has been generated with a very old version of ex_doc).

Alternatively there is also mix help which lists all available tasks from the current context with a brief description, mix help <taskname> prints full documentation for a given task.

1 Like

The command django-admin startproject mysite for create project “mysite”

The command django-admin startapp app_name for creates “modules”
Example:
django-admin startapp Blog - create Blog app on project “mysite”
django-admin startapp Forum - create Forum app on project “mysite”

After this commands i haw Blog and Forum on my project “mysite”

I am not using Django, but the closest I can think of is an umbrella application, in which You could build sub applications…

That would be

$ mix phx.new mysite --umbrella
$ cd mysite/apps
$ mix new blog
$ mix new forum

But it’s just one way to organize your project.

2 Likes

An app in elixir (on the BEAM in general) probably refers to something else as in Django context it seems.

Therefore we can only guess what you are looking for.

In elixir an application is the smallest unit of distribution. So every package you download from hex is an application on its own.

For Django, so it seems from podcasts I’ve listened to, an application seems to be some unit of code organisation with its own controller, models, and views. Even migrations?

If my understanding is correct, then the most equivalent is probably indeed another elixir app, that you can forward to in the router, and handle the routing from there. I have never built such an application. I doubt that there is a built-in generator for this, as it is quite uncommon to organize code like that in elixir.

It is more common to separate the Web-UI and business application rather than splitting the Web-UI.

2 Likes

SUMMARY
The closest to django apps in phoenix are contexts (when comparing the frameworks and not the ecosystem of python/elixir). Django apps could be “self-contained” (not necessarily though). They could include models, views, migrations, templates, urls, etc, and they could have everything they need to run inside a django project. Contexts, on the other hand, mainly contain the logic + schemas/models.

DETAILS
Keep in mind that what I’m going to write here is the default for both frameworks. Phoenix is way more flexible than django when it comes to folder/file structure.

django:

django-admin startproject project_name
cd project_name
django-admin startapp app_name

In django, you usually define the models inside app_name/models.py and probably add app_name to the list of INSTALLED_APPS in settings.py

phoenix:

mix phx.new project_name
cd project_name
mix phx.gen.html app_name model_name table_name field_one:string field_two:integer field_three:boolean

This generates a folder named app_name and a file called app_name.ex (context), both under the project_name/lib/project_name/ folder. The app_name folder contains your schemas or “models”. It will also generate the routes and migration files when necessary.

Example. Assuming you are inside a blog project:

mix phx.gen.html Accounts User users username:string email:string

would generate:

blog/lib/blog/accounts.ex
blog/lib/blog/accounts/user.ex

In django, the app has all the views and urls, etc. In phoenix, controllers (views in django) and routers (urls in django) are in separate places. All controllers are grouped together and all routes are usually in one file router.ex.

Also in phoenix, you have more options for generating different kinds of contexts out of the box. For example, you have phx.gen.html to generate an endpoint that deals with html, phx.gen.json to generate a json endpoint, phx.gen.schema to generate only the schema (model) with no views or urls, etc.

I’m sure I missed something while writing all this, but let me know if you need more/better explanations.

2 Likes