English 中文(简体)
Hx-push-url don't work on Back buttonpoint
原标题:Hx-push-url doesn’t work on back button click
  • 时间:2022-03-04 08:41:08
  •  标签:
  • django
  • htmx

I am trying to integrate Htmx with django and achieve single page application behaviour. I am rewritting Djangoproject.com poll app with htmx. When I click on detail page link, content loads, htmx push a new url in address bar. When I press back button, it took me to index page perfectly for the first time, after that if I again go to detail page and click back button, url shows of the index, button index content doesn’t load, content remains same as detail page. Here is my code

views.py

def index(request):
    latest_question_list = Question.objects.filter(pub_date__lte=timezone.now()).order_by( -pub_date )[:5]
    context = { latest_question_list : latest_question_list}
    if request.headers.get("Hx-Request") is not None:
        return render(request,  main/index/index.html , context)
    else:
        return render(request,  main/index/index-full.html , context)
    
    
def detail(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    print(request.headers.get("Hx-Request"))
    if request.headers.get("Hx-Request") is not None:
        return render(request,  main/detail/detail.html , { question : question})
    else:
        return render(request,  main/detail/detail-full.html , { question : question})
    

index.html

<div id="index" class="">

    {% if latest_question_list %}
    <ul>
        {% for question in latest_question_list %}
        <li><div class="has-text-link" hx-get="{% url  main:detail  question.id %}" hx-push-url="true" hx-target="#index" hx-swap="outerHTML">{{ question.question_text }}</div></li>
        {% endfor %}
    </ul>
    {% else %}
    <p>
        No polls are available.
    </p>
    {% endif %}

</div>

index-full.html

{% extends  base.html  %}
{% block content %}
{% include  main/index/index.html  %}
{% endblock content %}

detail.html

<div id="detail" class="">

    <form action="{% url  main:vote  question.id %}" method="post">
        {% csrf_token %}
        <fieldset>
            <legend><h1>{{ question.question_text }}</h1></legend>
            {% if error_message %}<p>
                <strong>{{ error_message }}</strong>
            </p>
            {% endif %}
            {% for choice in question.choice_set.all %}
            <input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}">
            <label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br>
            {% endfor %}
        </fieldset>
        <input type="submit" value="Vote">
    </form>

</div>


detail-full.html

{% extends  base.html  %}
{% block content %}
{% include  main/detail/detail.html %}
{% endblock content %}

我发现在浏览器中没有任何错误,在终点站没有错误。

现在,我知道,我可以把可以把我带上索引页的背顿语。 但是,用户却获得了这种使用,他们只是在其装置上使用背顿。

最佳回答

在用户点击背纽子时,添加这一javascript代码后,你可以重新上载:

var auxBack = performance.getEntriesByType("navigation");

if (auxBack[0].type === "back_forward") {
    location.reload(true);
}
问题回答

I face this problem in my Django application after a lot of searching I found the solution for this in the HTMX documentation by assigning the hx-history-elt you can lock it at hx-history-elt and it works for me grate





相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

热门标签