TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Django, HTMX and Alpine.js: Modern websites, JavaScript optional

450 pointsby czueover 3 years ago

39 comments

gavinrayover 3 years ago
Can someone genuinely explain to the the desire&#x2F;interest to use HTMX&#x2F;Hotwire&#x2F;LiveView etc?<p>I was writing web apps when rendering templated HTML views on your server was standard, and you had controller endpoints that returned HTML content, or things like Rails &quot;Unobtrusive Javascript&quot; where you had code like:<p><pre><code> $(‘#user-list’).html(“&lt;%= j render(‘index’, users: @users) %&gt;”) </code></pre> As an automatic callback to some action.<p>Whether or not you love or hate this model of development is irrelevant, because if at some point you need to render on a client other than a browser -- you have to start from the beginning and write an API now.<p>IE, so that your Android&#x2F;iOS app in Java&#x2F;Swift or whatnot, can ask your backend for data and render it using the tools available on that platform.<p>When we turned apps into pure API&#x27;s and just exchanged data with them, it opened up the ability to be platform-agnostic.<p>I guess I don&#x27;t understand why someone would choose to build an app that sends HTML over the wire instead of JSON&#x2F;XML&#x2F;whatever, that any client can render. All it means is that you have to write this functionality later when you want it, in addition to now having this HTML stuff.
评论 #29319459 未加载
评论 #29322048 未加载
评论 #29319774 未加载
评论 #29320050 未加载
评论 #29319768 未加载
评论 #29319502 未加载
评论 #29319341 未加载
评论 #29319855 未加载
评论 #29319449 未加载
评论 #29320847 未加载
评论 #29319348 未加载
评论 #29319373 未加载
评论 #29319980 未加载
评论 #29320441 未加载
评论 #29321335 未加载
评论 #29319520 未加载
评论 #29326528 未加载
评论 #29327031 未加载
评论 #29327864 未加载
评论 #29323048 未加载
评论 #29319310 未加载
评论 #29327498 未加载
polyrandover 3 years ago
After reading some HTMX criticism, there&#x27;s one point people seem to miss. Making HTML your application interface does *not* prevent you from having a JSON (or something else) API. If anything, it probably forces you to split your functions better. e.g:<p><pre><code> def get_user_data(user_id: int) -&gt; Dict (JSON-looking data): ... def render_user_view(user_id: int) -&gt; HTML: user_data = get_user_data(user_id) render_template(&quot;user_detail_view.html&quot;, context=user_data) </code></pre> If you need the user data in a JSON API, nothing prevents you from exposing `get_user_data` as a JSON endpoint. You can also use WebViews in a mobile app.<p>People tend to overestimate the &quot;interactivity&quot; needs of their apps and underestimate what they can achieve by just swapping HTML. HTMX also lets you swap &quot;Out of Band&quot; [0]. This makes it easy to model more complex interactions (like &quot;reactions&quot;), for example, updating a counter somewhere else in the app when a form is submitted. Reactive frameworks can also become a Rube Goldberg machine if an app is not properly designed from the beginning. Then you start fighting rendering loops, build dependencies, components&#x27; side effects, etc.<p>Personally speaking, HTML-driven apps are not just about easy vs. hard development, it&#x27;s also about your users [1]. Maybe a big React app runs fine on 8 CPU cores and 32 GB of RAM, but very often, your users just want to read some content, maybe submit a few forms, and leave. They may not want to download 2 MB of JS so that the page can render boxes with text, even more if your browser can already do that if you give it some HTML.<p>[0] <a href="https:&#x2F;&#x2F;htmx.org&#x2F;attributes&#x2F;hx-swap-oob&#x2F;" rel="nofollow">https:&#x2F;&#x2F;htmx.org&#x2F;attributes&#x2F;hx-swap-oob&#x2F;</a> [1] <a href="https:&#x2F;&#x2F;shkspr.mobi&#x2F;blog&#x2F;2021&#x2F;01&#x2F;the-unreasonable-effectiveness-of-simple-html&#x2F;" rel="nofollow">https:&#x2F;&#x2F;shkspr.mobi&#x2F;blog&#x2F;2021&#x2F;01&#x2F;the-unreasonable-effectiven...</a>
评论 #29323634 未加载
评论 #29324803 未加载
评论 #29327540 未加载
Mizzaover 3 years ago
I&#x27;ve been moving from Django to Elixir&#x2F;Phoenix&#x2F;LiveView and loving it so far. I hated the Angular&#x2F;React era of the web and mostly moved to doing backend development, but the Live App era has reinvigorated my interest in web development as a whole. I&#x27;ll miss Python a lot and hope they can come up with something that solves the concurrency issues, but Elixir is really pretty good in its own right.
评论 #29320209 未加载
评论 #29323757 未加载
评论 #29320272 未加载
评论 #29320242 未加载
评论 #29320884 未加载
评论 #29320752 未加载
amenodover 3 years ago
I agree that the way modern JS (need to?) download half the Internet for a semi-useful app is annoying, wasteful and dangerous.<p>However, I still remember the hell that was updating the state and keeping the rendering in sync with it when using jQuery. Native JS of course doesn&#x27;t improve on that, and (judging by the description - no experience with it) Alpine.js doesn&#x27;t either. For me, the paradigm of rendering from the internal state is what makes React (and Vue,...) useful. Too bad that npm ecosystem sucks and that everyone is inventing new shiny useless new things all the time... But React makes non-trivial single-page apps possible.<p>Of course, if you need server-side rendering (for example because of SEO) then the equation changes, and maybe using Alpine.js makes sense.
评论 #29322257 未加载
评论 #29322762 未加载
评论 #29322813 未加载
评论 #29322441 未加载
评论 #29322711 未加载
colbyhubover 3 years ago
Another one to consider is <a href="https:&#x2F;&#x2F;www.django-unicorn.com" rel="nofollow">https:&#x2F;&#x2F;www.django-unicorn.com</a> if you want that LiveView feeling for Django.<p>For my latest project[1], I&#x27;ve opted for <a href="https:&#x2F;&#x2F;unpoly.com" rel="nofollow">https:&#x2F;&#x2F;unpoly.com</a> instead of Alipine+htmx as Unpoly allows me to write 100% server-side code and sprinkle some progressive enhancement where desirable. As a result, I can offer a no-JS experience (minus the Stripe Checkout) for those who may want&#x2F;need it. Additionally, it forces me to focus solely on the server-side, and I write more idiomatic Django code as a result.<p>[1]: <a href="https:&#x2F;&#x2F;heraldsms.com" rel="nofollow">https:&#x2F;&#x2F;heraldsms.com</a>
评论 #29320896 未加载
评论 #29323566 未加载
robertoandredover 3 years ago
Don&#x27;t use a framework, use three!<p>This sounds like a lot of effort just to brag about how you avoided whatever piece of tech we&#x27;re considering to be evil this month.
评论 #29320481 未加载
评论 #29322491 未加载
评论 #29320535 未加载
resoluteteethover 3 years ago
One major downside to HTMX is that as soon as there is any state you have to keep track of, you&#x27;re teleported back to the late 90s or early 2000s where you have to keep track of all your state in hidden form fields, and you&#x27;re extremely constrained in how you update pages.<p>IMO, it&#x27;s annoying enough that it&#x27;s probably not worth it unless you&#x27;re doing something trivial. If you want to render html on the server you can still do that, but in many cases it is easier to just use custom javascript and maybe even receive the rendered html as json to simply updating pages rather than use htmx.
评论 #29321234 未加载
评论 #29320906 未加载
synergy20over 3 years ago
If I use alpine.js why do I need htmx still? I would use either of them but not both. Using both seems making a supposed-to-be-simple approach immediately back to complicated-again-now-you-need-two-components.<p>alpine increases client side interactivity, it can also do ajax to talk with server, why do I still need htmx then?<p>On the other hand if I use htmx I will probably use its hyperscript for interactivity on the client side, to be &#x27;consistent&#x27;.<p>Note both projects are trying to make frontend simpler, mixing them seems not to help that goal to me.
评论 #29320756 未加载
评论 #29319819 未加载
评论 #29319974 未加载
awinter-pyover 3 years ago
&#x27;django saas startup kit&#x27; concept of the author is really smart<p>there&#x27;s a hole in the market for people wanting to use frameworks to make saas sites -- frameworks provide great primitives that are subtly unusable for some modern web apps. Login but not saml, admin but not multitenant, forms but not multiple forms on a page<p>feels like this scratches an increasingly common itch of &#x27;the web as a platform isn&#x27;t shaped like what people use the web for&#x27;
评论 #29327877 未加载
btownover 3 years ago
An alternative that requires even less server-side work is to use PJAX <a href="https:&#x2F;&#x2F;www.npmjs.com&#x2F;package&#x2F;pjax" rel="nofollow">https:&#x2F;&#x2F;www.npmjs.com&#x2F;package&#x2F;pjax</a> which is the spiritual successor to Turbolinks from the early Rails days. It&#x27;s really a two-step process:<p>- create a quick JS file that loads and configures PJAX to automatically intercept links and - wherever you would have $(function() {}) ensure that it&#x27;s also triggered on the PJAX load event, and is idempotent (e.g. check if you initialized a component before initializing it again)<p>Then you just render pages server-side like it&#x27;s 2000, without any pjax-specific code needed in your templates themselves.<p>If you&#x27;re building a data-focused product either solo or with a small team, it allows you to focus on your Python code, and anything frontend is as easy as accessing the variable in a Django template. Of course, it&#x27;s a massive accumulation of technical debt - when you&#x27;re successful you&#x27;ll need to rewrite in a modern framework for maintainability and agility. But when you need to &quot;punch above your weight class&quot; and just get a product out into the world, it can be an amazing technique.
评论 #29320377 未加载
评论 #29319485 未加载
fareeshover 3 years ago
A suggestion for folks writing articles like this - sell me the advanced but typical example. I want to see an image input which is then previewed and then uploaded asynchronously with a loading bar and error states.
评论 #29324864 未加载
jonathan-adlyover 3 years ago
For people who want to see more advanced examples&#x2F;tutorials built using Django and htmx- you can see a bunch here: <a href="https:&#x2F;&#x2F;htmx-django.com&#x2F;" rel="nofollow">https:&#x2F;&#x2F;htmx-django.com&#x2F;</a><p>Anything that can be done with React&#x2F;Vue can be done with htmx in a more “Django-way”- it’s an amazing library that allows for complete web applications without the complexity of the JS ecosystem
评论 #29320517 未加载
jonatronover 3 years ago
Not sure about the snippet:<p><pre><code> &#x2F;&#x2F; when the DOM is loaded document.addEventListener(&#x27;DOMContentLoaded&#x27;, () =&gt; { &#x2F;&#x2F; find every element with the class &quot;close&quot; (document.querySelectorAll(&#x27;.close&#x27;) || []).forEach((closeButton) =&gt; { const parent = closeButton.parentNode; &#x2F;&#x2F; and add a &quot;click&quot; event listener closeButton.addEventListener(&#x27;click&#x27;, () =&gt; { &#x2F;&#x2F; that removes the button&#x27;s parent from the DOM parent.parentNode.removeChild(parent); }); }); }); </code></pre> It&#x27;d be clearer without useless comments and useless extra code:<p><pre><code> document.addEventListener(&#x27;DOMContentLoaded&#x27;, () =&gt; { document.querySelectorAll(&#x27;.close&#x27;).forEach((closeButton) =&gt; { closeButton.addEventListener(&#x27;click&#x27;, (event) =&gt; { event.target.parentNode.remove() }) }); });</code></pre>
jamesgeck0over 3 years ago
It&#x27;s worth noting the security tradeoffs of these micro-frameworks.<p>HTMLX uses innerHTML&#x2F;outerHTML extensively, meaning that XSS is a real concern. Any sanitation of user-generated content must happen server-side. This how traditional server-side frameworks generally work, but it&#x27;s the opposite of how sanitation tends to be handled in large JS frameworks such as Angular.<p><a href="https:&#x2F;&#x2F;htmx.org&#x2F;docs&#x2F;#security" rel="nofollow">https:&#x2F;&#x2F;htmx.org&#x2F;docs&#x2F;#security</a><p>Alpine.js requires an alternative syntax to avoid running afoul of unsafe-eval Content-Security Policy. With this more verbose syntax, there no inline expressions in templates; everything is bound to attributes in Alpine.data instead.<p><a href="https:&#x2F;&#x2F;alpinejs.dev&#x2F;advanced&#x2F;csp" rel="nofollow">https:&#x2F;&#x2F;alpinejs.dev&#x2F;advanced&#x2F;csp</a>
评论 #29327839 未加载
评论 #29328209 未加载
评论 #29324343 未加载
nop_slideover 3 years ago
Can anyone describe the use cases between HTMX&#x2F;Alpine and Unpoly JS? I see the 3 routinely mentioned, but I am unsure what coverage Unpoly has vs HTMX&#x2F;Alpine.<p><a href="https:&#x2F;&#x2F;unpoly.com&#x2F;" rel="nofollow">https:&#x2F;&#x2F;unpoly.com&#x2F;</a>
评论 #29339774 未加载
Savagemanover 3 years ago
It reminds me a bit of [MooTools Behavior](<a href="https:&#x2F;&#x2F;mootools.net&#x2F;blog&#x2F;2011&#x2F;12&#x2F;20&#x2F;mootools-behavior" rel="nofollow">https:&#x2F;&#x2F;mootools.net&#x2F;blog&#x2F;2011&#x2F;12&#x2F;20&#x2F;mootools-behavior</a>) (from 2011!) where interactivity was created by adding tags to HTML, similar to the article!
评论 #29324719 未加载
joseferbenover 3 years ago
I wrote about my experience evaluating the same stack here: <a href="https:&#x2F;&#x2F;www.joseferben.com&#x2F;posts&#x2F;hoarddit_a_website_to_discover_art&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.joseferben.com&#x2F;posts&#x2F;hoarddit_a_website_to_disco...</a>
streamofdigitsover 3 years ago
would it possible to have a browser <i>ship</i> with htmx so that it is possible to have truly &quot;javascript optional&quot;? (in principle this applies to any full js framework but I suppose it is natural in the htmx context)<p>are there any unsurmountable issues around security and the sandbox etc (not terribly familiar with browser internals)
评论 #29320982 未加载
评论 #29419631 未加载
beebeepkaover 3 years ago
I think it&#x27;s great people who passionately dislike JavaScript have such powerful options.<p>Personally, I am not buying the whole &quot;you can do anything this way&quot; because it seems to me the main driver, implied or plainly stated, is always the &quot;and no js!&quot; part.<p>I get it, though. We are all capable of going to great lengths to prove a point. Having more viable options is great.
评论 #29321423 未加载
ksecover 3 years ago
Sorry if this is off topic, I remember reading a proposal of including something similar to Alpine.js &#x2F; HTMX within the HTML5 spec.<p>But I lost the page. And the proposal itself doesn&#x27;t mention Alpine.js or HTMX so no keyword bring it up in Google. I am wondering if anyone have any idea.
评论 #29319533 未加载
评论 #29321685 未加载
strogonoffover 3 years ago
Downside of most HTMX examples (such as the form in TFA) seems to be lack of graceful degradation with non-JS-enabled user agents.<p>But then, the rest of the page is server-generated (taking care of SEO), and handling interactivity without JS may not be a priority for most sites nowadays.
评论 #29319908 未加载
cosmoticover 3 years ago
What makes it &#x27;modern&#x27;?
评论 #29327563 未加载
sbussardover 3 years ago
I always have the fear that something I write will be too complicated for someone else. This has become particularly irksome when integrating OIDC into a single page app. It’s gut wrenching to double check every random header for every domain it needs to serve, to set up SSL for localhost, to include the right headers on the fetch request, and maybe edit the &#x2F;etc&#x2F;hosts file to make it work. That process is a joy killer. The tools mentioned here can help stop that madness and bring joy back into development. So thank you! Please evangelize these ideas to browser standards committees and whoever will listen.
abishekgover 3 years ago
This is a pretty cool idea. The only catch is retaining consistent state across the page when there are multiple components that depend on same data. For instance, if there’s a form that submits to increment or decrement some dataset, but there is a stat based on that data in the sidebar, then it’s nearly impossible to keep the stat relevant. That said, its a planning problem than a stack problem.<p>I’ve been trying this in all my hobby projects and am absolutely hooked. It feels simpler than even django-sockpuppet, say.
austincheneyover 3 years ago
I’m not sure I understand the motivation to avoid JavaScript. I completely understand not wanting 400mb of SPA&#x2F;NPM nonsense, but otherwise what problem does avoiding JS solve in a webpage?<p>* Is it because JavaScript is too hard to learn or too unpleasant to write?<p>* Is it because training people to write JavaScript is too much effort?<p>* Is it due to a lack of trust in your fellow developers?<p>* Is it due to challenges in planning or forming architecture?<p>* Something else?
评论 #29332686 未加载
reidjsover 3 years ago
Can this stack easily handle 2-way data bindings on the frontend? For example, when you update a text input, the title of the text input changes?
评论 #29319770 未加载
评论 #29319847 未加载
评论 #29319276 未加载
chrisfinazzoover 3 years ago
Smells like Turbo (and related frameworks) to me, which isn&#x27;t a bad thing. From my brief use of CRA, I can see why many people are attracted to it, but the fact that a bunch of JS has to download before anything happens is never far from my mind.<p>In the meantime, I&#x27;ll keep waiting until we get native HTML imports&#x2F;includes - hopefully before the heat death of the Universe.
miki_tylerover 3 years ago
This is a very promising architecture! We are actually building a template for Kit55 (<a href="http:&#x2F;&#x2F;stack55.com" rel="nofollow">http:&#x2F;&#x2F;stack55.com</a>) that illustrates the use of Alpine,js. I think we should look into HTMX as well. Do you know if there is a comparison of Alpine.js vs HTMX somewhere?
评论 #29319832 未加载
评论 #29319268 未加载
sam_goodyover 3 years ago
There is a cycle that has existed for a while:<p>- Home computer.<p>- Mainframe. Much more powerful and reliable.<p>- More powerful PC. Mainframes have latency et all.<p>- More powerful Server, browser cannot handle that much computing.<p>- More powerful browser. A whole OS really. Servers have latency.<p>- More powerful server.. All that complexity is hard to rebuild on the client side, and besides phones are too slow...<p>Party on!<p>:D
bingohbangohover 3 years ago
I&#x27;m a big fan of these setups but has anybody ever successfully used a minimal-JS setup for anything useful?<p>I mean, there&#x27;s apps like Pinboard but React &amp; Redux do handle a lot of complexity and most modern apps demand that complexity.
评论 #29320851 未加载
评论 #29321942 未加载
评论 #29320376 未加载
ilrwbwrkhvover 3 years ago
Glad to see just a simple cdn script tag back in the play. Hopefully we can also pivot to smart documents rather than the web being an application platform.
theptipover 3 years ago
Based on the content of the last DjangoCon, it seems HTMX is the way the Django community is going.<p>Anyone have thoughts on HTMX vs django-reactor (which is a LiveView port)?
thih9over 3 years ago
Is anyone here using either HTMX or Alpine.js (or both) in production? What are your thoughts, are you happy with how these work in practice?
评论 #29320210 未加载
评论 #29321864 未加载
评论 #29324093 未加载
Wronnayover 3 years ago
<a href="https:&#x2F;&#x2F;archive.md&#x2F;9YAJm" rel="nofollow">https:&#x2F;&#x2F;archive.md&#x2F;9YAJm</a>
pjs_over 3 years ago
HTMX really kicks ass, I simply love it.
jpe90over 3 years ago
Any opinions on how this (or a PETAL stack) compares to using Clojure &amp; Clojurescript?
joelbluminatorover 3 years ago
I like how many frameworks end up cloning what Rails is doing (Stimulus &#x2F; Turbolinks etc).
1270018080over 3 years ago
This honestly sounds like a miserable tech stack.
nawgzover 3 years ago
I take so much issue with HTMX being branded as &quot;JavaScript optional&quot;. It&#x27;s a project built for people who hate JS by people who hate JS, and as the resulting developer workflows aren&#x27;t JS-based have managed to convince themselves they&#x27;re not using JS.<p>It&#x27;s totally false. Your HTMX app does not work for JS-disabled people any more than someone&#x27;s React&#x2F;Angular&#x2F;Vue first-render-on-server app.
评论 #29320449 未加载