News for hackers. Stuff that matters.
Re:Django 4.0 release candidate 1 released by creimers on Monday November 22, 2021 @05:29AM (#29304904)
I love Django and have been using it for many many years. Lately, however, I’ve been finding it increasingly difficult not to be annoyed by some aspects of the developer experience. For example I’d choose React + Typescript over Django templates any time of the day.
Re:Django 4.0 release candidate 1 released by pbowyer on Monday November 22, 2021 @05:50AM (#29305033)
You shouldn't be downvoted for saying this. As a server-side developer of 20 years the server-side templating languages have a lot to learn from the frontend experience. And I say this as a user of Smarty, Jinja2 and now Twig & nunjucks. Template inheritance is fantastic. But...

Being able to develop and test standalone HTML components using Storybook is a whole lot nicer than splitting a server-side template into components (which Twig certainly doesn't support cleanly), building a tool to render them and then maintaining your own component library. So normally we end up with HTML bits in Storybook which are copied and pasted into Twig templates, placeholders added and then you can guess they get out of sync.

I appreciate the argument that I should "use JS server-side and render ${js_fe_framework_of_choice} server-side" and it's valid. But I'd also like to be able to run components in _any language_ and get the same DX. Note none of this is talking about hydration - I'm wanting to leverage the JS FE frameworks as clever templating engines which output dumb HTML.

Re:Django 4.0 release candidate 1 released by emacsen on Monday November 22, 2021 @07:07AM (#29305497)
I'm talking entirely out of my rear end, but I'm curious if there is any validity to some of these projects that let you run pythonic code in the browser, either through Webassembly or brpython. For example, this projects https://github.com/stefanhoelzl/vue.py claims to let you write Vue.js in Python in the browser, which then gets turned into regular Vue.js components. I imagine a performance penalty, but a developer performance benefit of allowing something else to run on top and make a more composable experience.

Or is this barking up the wrong tree entirely?

Re:Django 4.0 release candidate 1 released by pbowyer on Monday November 22, 2021 @11:56AM (#29308569)
Up the wrong tree :) Vue.py gives you a way to write Vue to run in the browser using a Pythonic syntax. I'm talking about running actual Vue code (or anything else in that style) server-side as Django templates.
Re:Django 4.0 release candidate 1 released by globular-toast on Monday November 22, 2021 @06:29AM (#29305261)
> For example I’d choose React + Typescript over Django templates any time of the day.

What's stopping you? I use Django Rest Framework to write views and React + Typescript for the front end. Using templates is often way simpler, though.

Re:Django 4.0 release candidate 1 released by semitones on Monday November 22, 2021 @05:50AM (#29305034)
You could use Django just for the backend and have your frontend code be written in React+Typescript, no?
Re:Django 4.0 release candidate 1 released by dvdkon on Monday November 22, 2021 @06:00AM (#29305095)
But that means throwing out a large part of Django and writing lots of "interconnect" code. At that point I think it's better to choose a different backend, one that will generate typed Typescript client code (or use Typescript on the server and some clever metaprogramming).
Re:Django 4.0 release candidate 1 released by mracette on Monday November 22, 2021 @09:14AM (#29306467)
You can also just generate an Open API spec and use something like openapi-generators to produce typed client code for data fetching (https://github.com/OpenAPITools/openapi-generator)
Re:Django 4.0 release candidate 1 released by Pandabob on Monday November 22, 2021 @10:53AM (#29307715)
This has been such a life saver for me these past couple of months
Re:Django 4.0 release candidate 1 released by Zababa on Monday November 22, 2021 @07:22AM (#29305571)
The thing is that there's no TypeScript backend that gives you as much stuff out of the box as Django. You're either going to spend time on how to communicate between Django and TS, or spend time on how to recreate Django functionality in TS.
Re:Django 4.0 release candidate 1 released by Pandabob on Monday November 22, 2021 @10:52AM (#29307705)
Doesn’t Django Rest Framework solve most of the issues you raise?
Re:Django 4.0 release candidate 1 released by DylanSp on Monday November 22, 2021 @05:05PM (#29312087)
Not a Django person, but I've used a few server-side templating languages, and I agree. I'd love to go mostly/entirely server-side, cut down on boilerplate AJAX calls, and ship smaller/less JS-heavy sites to the browser. But I'd also like to write my view logic in a full language, with typechecking and a solid developer experience. I'm hopeful React Server Components will help with at least some of this.
Re:Django 4.0 release candidate 1 released by scrollaway on Monday November 22, 2021 @07:01AM (#29305456)
Django templates are the worst.

Django is great. Just don't use its templates or form systems. Indeed react is a great replacement for both.

Re:Django 4.0 release candidate 1 released by sgt on Monday November 22, 2021 @07:06AM (#29305486)
I disagree with that. They are fine for regular forms, tables (as in grids), etc. I usually have a few views using plain old Django templates (with a couple of template tags for additional functionality), and then maybe one view with a full blown SPA in it. No need to build your login page etc in React.
Re:Django 4.0 release candidate 1 released by mejutoco on Monday November 22, 2021 @07:10AM (#29305512)
I disagree. The way you can extend sections of other templates without creating an inheritance chain works very well (and I missed in template systems, for Java, for example)
Re:Django 4.0 release candidate 1 released by scrollaway on Monday November 22, 2021 @07:36AM (#29305681)
To be clear, I use them as well. And every time I come to regret it unless it's on a site I absolutely do not touch once it's finished.

Templates make it harder to find and debug issues. They have extremely little tooling to give assurances about what they're compiling, whether the syntax is correct etc. This requires you to build massive, flaky test suites.

I'll take react and typescript any day over even one tenth of that.

Re:Django 4.0 release candidate 1 released by mundanevoice on Monday November 22, 2021 @08:49AM (#29306252)
Well, It is more of a design problem than a Django template problem. If you add more and more logic in the template itself instead of dealing with it in the views context or templatetags, you are bound to find yourself in such position that you have very hard to test code.

The tooling is fine and almost always lets you know where you are breaking stuff. Also, comparing react tooling with it is not fair, it is a much modern ecosystem that has pitfalls of its own.

I have been writing Django professionally for 10 years and React + Django together for 4+ years, so I really know what I am talking about here.

Re:Django 4.0 release candidate 1 released by mejutoco on Monday November 22, 2021 @12:42PM (#29309253)
I basically agree. Templates are basically strings, while jsx is in the end a full programming language (js).

It has the downsides you mention (the error reporting is okayish I think). On the other side, you can write html directly (no classname, etc), and there is no transpiling/build tools/server-side rendering to add. String templates are also side-effect free.

For spa I would prefer React (with Typescript): for a request/response old-fashioned app, or something like htmx/hotwire text templates can be very productive. I see your point though.

Edit: extra points.

Re:Django 4.0 release candidate 1 released by candiodari on Monday November 22, 2021 @08:18AM (#29306011)
Wouldn't all of python have that problem? What you're saying is that there's very little static checks until the code is run, but all of python has that problem too, doesn't it?

Either you write big test cases covering everything, or you live with the knowledge that very unexpected things can be passed in to your methods without you getting notified.

And then there's javascript ...

Re:Django 4.0 release candidate 1 released by dangerbird2 on Monday November 22, 2021 @12:26PM (#29309003)
It's less about static checking than it is runtime debugging. When you have a syntax error in a template, Django's error reporting is super opaque about the problem, usually just raising a TemplateSyntaxError with a vague description. This just gives you the python error stack with little information of the actual context of the actual problem in the template itself.

By contrast, if you have a problem in the python code, the exception stacktrace will point you to exactly where the problem originates. If you add a `breakpoint()` call in the source before the exception point and run the request again, you'll get dropped into a live debugging repl

Re:Django 4.0 release candidate 1 released by scrollaway on Monday November 22, 2021 @12:14PM (#29308820)
Python doesn't have the problem as much. It does to an extent, though it's getting better with type hinting nowadays.

I do prefer working with typescript than with python but I still rather work with Python than with django templates which are way worse.

Re:Django 4.0 release candidate 1 released by ekspreso on Monday November 22, 2021 @09:02AM (#29306357)
Have you tried https://pebbletemplates.io/ for Java?

I haven't used it extensively, so I can't speak how it compares to Django in larger setups.

Re:Django 4.0 release candidate 1 released by mejutoco on Monday November 22, 2021 @12:37PM (#29309169)
Indeed! That is the one template language that looked like it might behave in the same way.
Re:Django 4.0 release candidate 1 released by Hamuko on Monday November 22, 2021 @07:20AM (#29305565)
How many tools do I need to install to publish a front-end made with React?
Re:Django 4.0 release candidate 1 released by scrollaway on Monday November 22, 2021 @07:42AM (#29305741)
Just the one if you're using CRA. None if you're embedding react as is.

The answer depends on how you're using react, and what advantages you want to get from it. "How many tools" is not a real question to ask. If you're concerned about ease of development and learning curves, then that gives you an actual goal to optimize for, and it won't coincide with the amount of tools (react is harder to use embedded than with tooling, hence why the latter is more popular).

Re:Django 4.0 release candidate 1 released by Hamuko on Monday November 22, 2021 @08:17AM (#29306000)
For each and every tool that is added to my project, it needs to be installed by every developer, it needs to be understood at least on some level by every developer, and it needs to be maintained by someone.
Re:Django 4.0 release candidate 1 released by scrollaway on Monday November 22, 2021 @12:16PM (#29308840)
You're missing my point. I don't know if this conversation can go anywhere.
Re:Django 4.0 release candidate 1 released by Scarbutt on Monday November 22, 2021 @09:53AM (#29306873)
Just one, esbuild for JSX and bundling.
Re:Django 4.0 release candidate 1 released by 8589934591 on Monday November 22, 2021 @10:17AM (#29307185)
I love Django a lot too. It has made MVPs really easy to deliver. At the same time, I am interested in understanding about the disadvantages/quirks of using Django. Do you have more examples that you could share?
Re:Django 4.0 release candidate 1 released by sgt on Monday November 22, 2021 @05:51AM (#29305042)
Nothing stopping you from writing a Django app with parts written in React, VueJS or whatever you prefer.
Re:Django 4.0 release candidate 1 released by creimers on Monday November 22, 2021 @06:57AM (#29305432)
Obviously nothing is stopping me, that's not the point. I was just highlighting the fact that in my opinion the DX of parts of Django is inferior compared to the JS frontend ecosystem. As the Django community, I think this is something we should have an eye on.
Re:Django 4.0 release candidate 1 released by sgt on Monday November 22, 2021 @08:03AM (#29305909)
You're absolutely right in the sense that the Django and other communities should not stagnate and should also keep an eye on what the other frameworks are up to. Maybe the default template engine should be replaced in Django?
Re:Django 4.0 release candidate 1 released by rglullis on Monday November 22, 2021 @08:57AM (#29306313)
Template engines are already configurable and pluggable for quite a while.

The "problem" is that most of the third-party apps that want to provide templates end up using the default, so either you have to reimplement those or stick with the default engine.

Re:Django 4.0 release candidate 1 released by ensignavenger on Monday November 22, 2021 @09:38AM (#29306705)
You can use both the Django Template Engine and other template engines side-by side in the same project. You can then write your custom templates in whatever you want, and have your third-party apps use whatever template engine they want to.
Re:Django 4.0 release candidate 1 released by rglullis on Monday November 22, 2021 @11:44AM (#29308416)
My comment was more in the sense of wanting to extend third-party templates, but yeah, if you don't need to touch them you should be fine as well.