Hey,
I’m new to Django and I’m implementing HTMX on my project.
There is a view that only allows POST request via this decorator for an async HTMX function to work.
from django.views.decorators.http import require_POST
@require_POST
However I have i18n activated and due to the i18n_patterns in my root URL I will make a GET request on top of the POST request. I have confirmed this by turning off i18n. It worked then and produced no GET request.
Can I somehow tell the i18n_patterns to ignore that route so I can have both working?
In case it is needed. The code is from the django-htmx example project :
views.py
@require_GET
def csrf_demo(request: HtmxHttpRequest) -> HttpResponse:
return render(request, "csrf-demo.html")
@require_POST
def csrf_demo_checker(request: HtmxHttpRequest) -> HttpResponse:
form = OddNumberForm(request.POST)
if form.is_valid():
number = form.cleaned_data["number"]
number_is_odd = number % 2 == 1
else:
number_is_odd = False
return render(
request,
"csrf-demo-checker.html",
{"form": form, "number_is_odd": number_is_odd},
)
urls.py
..
urlpatterns = [
path("", views.index),
path("csrf-demo/", views.csrf_demo),
path("csrf-demo/checker/", views.csrf_demo_checker),
]
...
root\urls.py
...
urlpatterns = i18n_patterns(
path("", include("pages.urls")),
path("admin/", admin.site.urls),
)
...
csrf-demo.html
...
<form hx-post="/csrf-demo/checker/" hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}' hx-target="#result">
<label for="id_number">
Number:
</label>
<input id="id_number" name="number" type="text" placeholder=""></input>
<button type="submit">
Check if odd
</button>
</p>
</form>
...
csrf-demo-checker.html
{% if not form.is_valid %}
Please enter a number
{% elif number_is_odd %}
{{ form.number.value }} is odd!
{% else %}
{{ form.number.value }} is not odd.
{% endif %}