Coder Social home page Coder Social logo

Modal Create/Update example about django-htmx HOT 3 CLOSED

xtlc avatar xtlc commented on August 28, 2024
Modal Create/Update example

from django-htmx.

Comments (3)

adamchainz avatar adamchainz commented on August 28, 2024

there is no way to send a "name" attribute via hx-get in a button to the view

You can use hx-vals to send extra query params: https://htmx.org/attributes/hx-vals/

But a second view is the right approach!

i used the DRF API to POST new products into the database

Just make a normal view that returns HTMl. That's what HTMX is looking for and it can still use it without a page reload. There's no need to use JSON.

As a design pattern I try to avoid modals. I prefer placing standalone forms on their own pages so they have their own URL's, obey the forwards/back buttons, etc. Because of this, and how good the htmx modal page is, I'll skip adding an example here.

Hope that helps,

Adam

from django-htmx.

xtlc avatar xtlc commented on August 28, 2024

Thanks about the reassurance with the second view.

Tried to get it working with a second template view, but I struggle to update the htmx part:

class CreateProductView(TemplateView):
    def get(self, request, *args, **kwargs):
        return render(request, "app/product_create.html", {'form': ProductForm})
    
    def post(self, request):
        Product.objects.create(...)
        return render(request, "app/index.html")

this way I can post data without using the DRF, but I still do not see immediately the updates posted.

from django-htmx.

adamchainz avatar adamchainz commented on August 28, 2024

Your response contains HTML, so if it's working then htmx can place that HTML in the DOM. Try debugging with your browser's devtools to see what happens in the network/DOM tabs. Also you can try the htmx debug mode: https://htmx.org/docs/#debugging

from django-htmx.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.