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

    limited-diamond-93357

    12/31/2021, 1:18 AM
    Are you asking about when it's not htmx that's making the POST?
  • l

    limited-pillow-24427

    12/31/2021, 1:18 AM
    when not is making POST
  • l

    limited-diamond-93357

    12/31/2021, 1:19 AM
    For example?
  • l

    limited-pillow-24427

    12/31/2021, 1:20 AM
    Copy code
    <li>
            <input type="checkbox" id="checkbox-done-{{task.id}}" value="" checked="checked"
                hx-post="{% url 'complete' task.id %}" hx-trigger="click">
            <label for="checkbox-done-{{task.id}}"><del> {{ task.name }} </del> </label>
            <a href="#" role="button" class="delete-button" hx-post="{% url 'delete' task.id %}">x </a>
  • l

    limited-diamond-93357

    12/31/2021, 1:20 AM
    (And if you're handling a form submit with htmx, it's not the form that's doing the request, it's htmx.
  • l

    limited-diamond-93357

    12/31/2021, 1:21 AM
    Because you're using
    hx-post
    , the
    addEventListener('htmx:configRequest',
    means the request will include the CSRF token.
  • l

    limited-pillow-24427

    12/31/2021, 1:24 AM
    sorry, but dont understand this one, probably because lack of my knowledge
  • l

    limited-pillow-24427

    12/31/2021, 1:30 AM
    CSRF token is something that is attatched to? Why I can do one of
    hx-post
    with csrf token, when another
    hx-post
    cant see that/didnt find token on the same page?
  • l

    limited-pillow-24427

    12/31/2021, 1:33 AM
    repo: https://github.com/sgazda94/Pico-ToDoX (little messy now)
  • u

    user

    12/31/2021, 9:21 AM
    Hello All, Today is my day 1 of learning HTMX. I've been working with Django for over 2 years now. I want to create a simple POST request using HTMX. For this, I've removed method and action attribute from my form ( made using cripsy forms ) I've also added hx-post and hx-target attributes to my submit button using jquery as the submit button is also getting created as a part of crispy forms On submit, I was expecting a POST request to be sent to the url provided in hx-post Instead, I see a GET request being sent to the current URL.
  • u

    user

    12/31/2021, 9:22 AM
    Can you please tell me where did i go wrong in this entire process ? I've added the HTMX CDN to my base.html file which is being used in all templates
  • p

    prehistoric-cat-63987

    12/31/2021, 10:05 AM
    Things to check: are you connected to internet? So the CDN would be able to pick up? hx-post should be mapped with URL the same way you would map url to action. all hx attributes should be placed in the form tag itself. Then attributes
  • p

    prehistoric-cat-63987

    12/31/2021, 10:07 AM
    I'll recommend this tutorial before you approach doing it by your self https://youtube.com/playlist?list=PL-2EBeDYMIbRByZ8GXhcnQSuv2dog4JxY
  • u

    user

    12/31/2021, 10:07 AM
    This is the exact tutorial I'm following right now. In the third video at 7:13, the guy is adding hx-post to submit button
  • u

    user

    12/31/2021, 10:08 AM
    he goes to add hx-post and hx-target into the submit button while the form does not have any attribute
  • p

    prehistoric-cat-63987

    12/31/2021, 10:09 AM
    Let me check
  • u

    user

    12/31/2021, 10:10 AM

    https://www.youtube.com/watch?v=H_m1g8XOtHY&list=PL-2EBeDYMIbRByZ8GXhcnQSuv2dog4JxY&index=3▾

  • u

    user

    12/31/2021, 10:13 AM
    I've tried your suggestion of adding hx-post and hx-target attributes in form instead of button, that didn't work too 😦 in both the cases, the POST request gets sent to the current url instead of the url mentioned in hx-post
  • u

    user

    12/31/2021, 10:24 AM
    My findings: hx-post works in form when the form is submitted using a button.
  • u

    user

    12/31/2021, 10:24 AM
    crispy form by default creates a submit button but the form is actually a input tag with type='submit'
  • u

    user

    12/31/2021, 10:24 AM
    I might be wrong here but hx-post in a button works but hx-post in an input created by cripsy form did not work
  • u

    user

    12/31/2021, 10:39 AM
    @User any luck ?
  • c

    cool-camera-13454

    12/31/2021, 10:58 AM
    I think there might be a confusion between htmx POST requests (performed with AJAX) and regular
    <form>
    POST requests (performed with regular HTTP requests). In your example code, I believe your
    <form>
    is submitting a regular HTTP request, even if you use the
    <hx-post>
    attribute. I think that because your
    <button>
    has
    type=submit
    which inside a
    <form>
    submits a regular POST request. You can check this by looking at the request headers, and seeing if the htmx headers (especially
    HX-Request:true
    ) are present. Since you are using Django's
    {% csrf_token %}
    template tag inside the form, that would explain why your csrf_token is included in your request, which makes the Django middleware accept it and not reject it due to a possible CSRF attack. For your other elements POSTing with
    hx-post
    , I am not sure why the CSRF token is not included with them. You mention you are including the CSRF token with the JS event listener. This should suffice to have your htmx POST requests include the CSRF token. Django usually sets a cookie with a CSRF token, so that the token is available for subsequent POST requests. Maybe that cookie is not being set for your case? For this case I tried your code on my side and it seemed to work. I made some changes to simulate the completion and deletion of tasks, but the POSTing seems to work on my side. Maybe you can try my code and see if it works for you
  • c

    cool-camera-13454

    12/31/2021, 11:00 AM
    Here the files
  • c

    cool-camera-13454

    12/31/2021, 11:03 AM
    although now that I realize, it seems the
    XCSRFToken
    is not the same as the cookie with the CSRF token. It seems a new value is created each time for the first, whereas the latter remains unchanged as long as the cookie is valid...
  • c

    cool-camera-13454

    12/31/2021, 11:11 AM
    Maybe the cookie is not that important for AJAX POST requests, and CSRF new tokens are always produced for each template render?
  • l

    limited-pillow-24427

    12/31/2021, 11:12 AM
    @User lots of information here, thanks, need to look at it and maybe read more about HTTP protocol and all that stuff It's also weird for me that it was working properly like 3 days ago, but yesterday started that csrf problem
  • c

    cool-camera-13454

    12/31/2021, 11:17 AM
    No problem! I agree, that is weird if it stopped working all of a sudden. Especially if you didn't change any configuration on Django or anything like that
  • u

    user

    12/31/2021, 11:19 AM
    @User htmx noob here. unable to do send a simple POST request using HTMX + Django + Crispy Forms. help me senpai /\
  • c

    cool-camera-13454

    12/31/2021, 11:25 AM
    Hi! I don't know if I'm knowledgable enough to be a senpai but I'll try my best 😁 Do you have any code snippet or repository you can show, where you are having this issue?
1...313233...100Latest