Web-development – Django – separation from the frontend (Templates)

angularjsdjangoweb-development

Long story short, let's assume I want to create a webpage that would enable the users to sell used cars.

I am a beginner to webdev, so pardon my ignorance. Fun fact: two months ago I knew literally nothing, I considered HTML as a relic of 90s, not in use anymore LOL.

  • I've been told I need something for the BACKEND and for the FRONTEND
  • As I know python pretty well I went for django for the backend
  • As I knew literally nothing about the frontend, my friend suggested
    me AngularJS

I have some doubts whether I'm heading the right direction.

Django is a backend framework, but my feeling during the tutorials was that the Templates part from the MVT pattern was pretty much the frontend, strongly dependend on the Models and Views. I've spent hours of googling for various terms and found this post on reddit/django – django best practises:

Treat the front end like a separate application. Keep it outside the Django project directory.

So with this approach (just to make sure I understand you), you are not using templates at all, correct?"

You are correct. Not using the templates at all.

How do I do that? I mean, the templates use all those template-tags, how do I replace django templates with Angular code?
I feel like I should use some REST API (how? django REST framework?) to communicate the Frontend with Backend. Write only the Models and the Views and connect to them using some POST and GET requests. Like

FrontEnd <—> REST API <—> Django Models and Views <—> Database

Then frontend uses its own logic and sends requests like

  • GET give me all the cars a user has posted
  • GET user's details
  • POST create a new user
  • POST a change to a car
  • GET query for all red cars

etc.

Then, another question about the frontend. I fell the Angular is not a good choice. It's purpose is to write Single Page Apps, which my clearly won't be. I can imagine a user, querying for some given cars criteria and then opening all of the offers in separate tabs.
Is it possible to use Angular for my usecase? I'm lost here 🙁

If I decide to stick to the django Teplates, how do I use AJAX in there?
And is there a way to make a hybrid of Single Page Apps and Multi Page Apps – to generally follow multiple templates, but keep parts of them SPA-like?
E.g. A main template used for querying for cars to browse, which would not trigger page reload, instead it would send an asynchronous request, display the spinner and then render the list of available cars. After clicking on a particular car, a new tab would be opened with details on that car (Details template).

I would highly appreciate any help on this frontend/backend separation.

Best Answer

So you need to show different states of your application -- a landing page, a list of cars, a single car's details, a procedure with a set of forms and prices etc for when you actually buy the car, et cetera.

There are two basic approaches: either have a single "page", a single HTML file with a load of Javascript that constantly updates the page and shows you the different things that need to be shown, or have generate separate HTML for each view on the server side and use links to URLs to have the browser go to each page.

If you choose the first, you use some frontend framework like Angular or React and only ask Django for data, typically JSON using REST (see Django REST Framework). The second, you use Django templates.

That's the basic choice -- do you build the interface in the web browser and only call the server for data, or do you build the interface on the server and call the server for HTML pages.

I think that with your level of experience, using Django templates to create HTML pages is easier; making a single page app probably more than doubles what you need to learn, and there is so much already. A web page to sell used cars can be made perfectly well using Django views and templates.

You can still have some Javascript where needed, and let Ajax call Django views that return bits of JSON, possibly using Django REST framework.

Related Topic