发布后视图不令人耳目一新

时间:2016-05-10 12:43:34

标签: python django internet-explorer firefox

我使用简单的表格来编辑发票名称(invoice_text)。当我提交更改时,它会重定向回索引页面。问题是索引页面显示旧记录。这仅适用于Firefox。 Internet Explorer直接显示更改。 F5有所帮助,但当然需要显示新的(编辑过的)信息。

forms.py

class InvoiceForm(ModelForm):
    class Meta:
        model = Invoice
        fields = ('Invoice_text',)

views.py

def index(request):
    latest_invoice_list = Invoice.objects.order_by('-pub_date')[:5]
    context = {'latest_invoice_list': latest_invoice_list}
    return render(request, 'invoices/index.html', context)

def invoice_edit(request, pk):
    obj = get_object_or_404(Invoice, pk=pk)

    if request.method == "POST":
        form = InvoiceForm(request.POST, instance=obj)
        if form.is_valid():
            obj = form.save(commit=False)
            obj.Invoice_text = request.POST['Invoice_text']
            obj.save()
            return HttpResponseRedirect('/invoices/')
    else:
        form = InvoiceForm(instance=obj)
    return render(request, 'polls/edit_Invoice.html', {'form': form})

Index.html模板

{% if latest_invoice_list %}
    <ul>
    {% for invoice in latest_invoice_list %}
        <li><a href="/invoices/{{ invoice.id }}/">{{ invoice.invoice_text }}</a> |  <a href="{% url 'invoice_edit' pk=invoice.pk %}">edit</a></li>
    {% endfor %}
    </ul>
{% else %}
    <p>No polls are available.</p>
{% endif %}

1 个答案:

答案 0 :(得分:2)

这很奇怪,但您应该可以使用@never_cache装饰器来阻止它:

from django.views.decorators.cache import never_cache

@never_cache
def index(request):
    ....
相关问题