https://htmx.org logo
Join Discord
Powered by
# 🔥-django-htmx
  • h

    hundreds-camera-24900

    11/16/2021, 1:39 AM
    the most obvious case of this is "load a form page, if it's an htmx request the form is being submitted so only return the form itself, if it's not an htmx request then it's a fresh request and give the site chrome back"
  • h

    hundreds-camera-24900

    11/16/2021, 1:39 AM
    so right now my usecase for the hx-retarget view is
  • h

    hundreds-camera-24900

    11/16/2021, 1:40 AM
    form page "anime-create" and detail page "anime-detail" - anime create form checks if it's an htmx view and if it's not gives a fresh form + site chrome
  • h

    hundreds-camera-24900

    11/16/2021, 1:40 AM
    if it is, give JUST the form assuming the form is being submitted and needs validation
  • h

    hundreds-camera-24900

    11/16/2021, 1:40 AM
    if the form is valid, then issue a 302 to "anime-detail"
  • h

    hundreds-camera-24900

    11/16/2021, 1:41 AM
    and the old issue was the anime detail showing up in the slot where the form would go for validation - that is now fixed because anime-detail includes the hx-retarget header
  • h

    hundreds-camera-24900

    11/16/2021, 1:41 AM
    but that means that anime detail will now ALWAYS be full page
  • m

    mysterious-toddler-20573

    11/16/2021, 1:41 AM
    why issue the 302?
  • h

    hundreds-camera-24900

    11/16/2021, 1:41 AM
    that's Django's normal behavior
  • m

    mysterious-toddler-20573

    11/16/2021, 1:41 AM
    Why not render the template directly?
  • h

    hundreds-camera-24900

    11/16/2021, 1:41 AM
    form submits w/ a post, 302 to a "success url"
  • h

    hundreds-camera-24900

    11/16/2021, 1:42 AM
    because it's pretty annoying tbh 😦
  • h

    hundreds-camera-24900

    11/16/2021, 1:42 AM
    that would be abetter solution
  • m

    mysterious-toddler-20573

    11/16/2021, 1:42 AM
    🤔
  • m

    mysterious-toddler-20573

    11/16/2021, 1:42 AM
    OK
  • h

    hundreds-camera-24900

    11/16/2021, 1:42 AM
    because it's a single request so obviously more efficient
  • h

    hundreds-camera-24900

    11/16/2021, 1:42 AM
    but django cares deeply about http verb in a lot of cases
  • h

    hundreds-camera-24900

    11/16/2021, 1:43 AM
    so if you post to view a, and then want to call+return view b you'd need to synthase a fake request to act as the get request
  • h

    hundreds-camera-24900

    11/16/2021, 1:43 AM
    or you have to duplicate the work to get the context of view b so it can be called directly in the form_valid response from view a
  • h

    hundreds-camera-24900

    11/16/2021, 1:44 AM
    either way you either need to do a lot of work to fake a request, or move the work that normally lives in the views to reuseable functions
  • h

    hundreds-camera-24900

    11/16/2021, 1:44 AM
    which is possible but not ideal because it's not "django-y"
  • h

    hundreds-camera-24900

    11/16/2021, 1:45 AM
    this is 100% not htmx's fault it's just an artifact of django wanting all form submits to act the same way
  • h

    hundreds-camera-24900

    11/16/2021, 1:47 AM
    so wrapping it back to the header, view b is forcing a full body load, which will work great until for some reason I want to add a form to that page and self submit or something
  • h

    hundreds-camera-24900

    11/16/2021, 1:47 AM
    I think I'm overthinking this
  • h

    hundreds-camera-24900

    11/16/2021, 1:47 AM
    I think the answer there is obvoiusly just make a new view that only handles the form submit
  • h

    hundreds-camera-24900

    11/16/2021, 1:48 AM
    the detail page is locked at being detail and won't mutate in the future to being a create/update view, and if you DO need that case just move it over to the side and handle it separately
  • h

    hundreds-camera-24900

    11/16/2021, 1:48 AM
    and the same hx-retarget will work for that submit the same way it will for the first view
  • b

    brave-raincoat-81051

    11/16/2021, 11:26 PM
    I didn't read all the details but so far I've created a view specifically for handling the form submit and its worked really well
  • h

    hundreds-camera-24900

    11/17/2021, 1:49 PM
    That's where I landed too - don't try to do too much with a view
  • v

    victorious-room-74725

    11/17/2021, 3:41 PM
    Having a larger number of smaller, specialized views is the way to go with Django + htmx
1...181920...100Latest