Django as a backend

Hi everyone,

Before I dive to deep I wanted to ask a question just to make sure what im thinking is even possible. Even if it is possible what would be better

What I’m trying to do:

Im working on a startup project with a friend he is a senior python developer and uses Django on a daily basis. I’m a DevOps engineer and mostly work on pipelines to auotmate so I don’t really have any frontend development skills. I discovered anvil.works and suggested that we attempt to use anvil as our frontend

What I’ve tried and what’s not working:
I wouldn’t say we have tried much yet, thats why im starting here but what my current plan is:

  • Host my own postgres server
  • Have a live Django env maintaining the models and db
    on that postgres cluster etc. and Django is the backend
  • Write anvil frontend code to call these backend
    functions either by exporting the anvil code into the
    django app directory or to have it completely seperated
    and called as an uplink.

The real problem at this stage is hosting the application with all of this consider im aware of the fact that anvil provides the hosting etc. but the hosting costs are quite expensive for a South African and Im capable of hosting and exposing apps on my local server especially just in the startup phase.

Another benefit of this is it will allow me to have multiple frontend options because the backend functions would essentially all be API based calls then.

Any input would really be appriciated

It sounds like you want the Django server to essentially take the place of Anvil server functions, data tables, and other services. That’s definitely possible, to have the Anvil forms make API calls out to a separate server using anvil.http.requests: Anvil Docs | Making HTTP requests

You’d lose some of the built-in integrations, like the Users service (which relies on Anvil’s data tables), scheduled tasks (which you’d do however Django does them), etc. The Django server would need to build out APIs for any of the services you want to use…authentication, authorization, email, etc.

Whether the cost (time and effort) of making that work is worth the benefits you’d get out of it is up to you. Your Django developer friend is probably used to using Django to create the front-end, too, so creating a server API only environment might not be leveraging his experience well.

So possible, yes.

1 Like

Thanks for the quick reply @jshaffstall the google auth proccess was one of the main attractions to anvil. So would using django as a backend not allow this functionality to work? If I make the same table in my own database using django and trying to use the anvil user functions fetching the required data from the db would that not still be an option?

Is it possible?

Maybe it is, but it feels like buying an electric car, replacing the batteries with a tank and the electric motor with a gasoline one. Then you realize you need the transmission, then you need better brakes, then…

At the end you have a patchwork, you miss the flexibility of your Django apps on the front end and the convenience of using Anvil and all its magic.

My opinion is that if you want power and flexibility, you should go the Django way, manage your own database, server, security, the whole stack. If your target is working in an environment secure, easy to use, with higher productivity and you are OK with sacrificing some flexibility, then Anvil is the right tool.

If you hit a roadblock with Anvil, for example if you want to use your own database, then you replace the database. This works, has been done many times, I’ve seen people talking about similar configurations here in the forum.

But using Anvil only for the front end feels wrong. I haven’t tried, maybe it’s possible, but…

1 Like

The Users service is tied to Anvil’s data tables. I don’t know of any way to tell it to use a table in another database instead.

As Stefano says, when you replace the backend with something else, you’re going to lose the convenience of the various integrations that makes Anvil so productive.

1 Like

Hi @Duard ,

I use Anvil as a front end + services together with my own backend, pretty much like you described. I chose Anvil because I didn’t want to do HTML/css/javascript and the hosting.

This is what I do:

  • I use Uplink to call my backend functions from the frontend. This is simple and works well. I run my server code on an AWS Lightsail instance in the same datacenter as Anvil hosts the apps (London, eu-west-2) to minimize latency.
  • I use SqlAlchemy ORM together with an AWS hosted MySQL database (in the same datacenter)
  • I use the Anvil Users service in my backend and that works well. I store the user information in a table in my own DB, and a reference to the user ids in that table in the Anvil Users table.

Things I like with this approach:

  • Most of my code is in the backend, and I can work with that code in PyCharm.
  • You can work with all Anvil services from the backend, just as you would in a server module in Anvil.
  • I prefer working with an ORM on top of my own database for complex datamodels.
  • As you mention, this allows for a decoupled design where you keep as much logic as possible in the backend, and then have a light, replacable frontend. If my backend in the future will work with a different frontend, then I will change my Uplink functions (all collected in one switch-yard file) with APIs, and a couple of files that interfaces the Anvil services.

I think using Django as a backend that communicates with Anvil via an Uplink will work just fine.

4 Likes

This is just an idea, not necessarily a great one…

A hybrid approach would be to have your Django developer develop his Django website, then imbed anvil apps as i-frames that you would be able to populate with your UI logic, using your skills to access data from the Django back-end machine through uplink.

In this way your friend can be comfortable building out the “large brush strokes” of a site, while you focus on specific end-user logical interactions, like for example, a user returns process for an e-commerce site.

This would allow you to use your data-gathering skills to “put buttons on a screen” without having to learn an entirely new set of web skills.

1 Like

Hi, Stein!

With this approach using SqlAlchemy, can you use autocomplete in Anvil Builder?

Hi @movelabs,

I use SqlAlchemy in my backend server code that is separate from Anvil (connects to Anvil via uplink). So I work with it in PyCharm and not in the Anvil Builder.

1 Like