django doesn't update html template after `model.save()`

  • Last Update :
  • Techknowledgy :

Try adding the "never_cache" decorator to the view reponsible for page rendering:

from django.views.decorators.cache
import never_cache

@never_cache
def myview(request, ...):
   ...

or to the dispatch method if it's a CBV:

class MyView(View):

   @never_cache
def dispatch(self, request, * args, ** kwargs):
   return super().dispatch(request, * args, ** kwargs)

Suggestion : 2

Try adding the "never_cache" decorator anycodings_javascript to the view reponsible for page anycodings_javascript rendering:,I have a Django application using Django anycodings_python REST Framework. When I do an Ajax call that anycodings_python changes a model field:,When I modify the objects using a regular anycodings_python Django form (for example when create an anycodings_python object), the page reloads properly.,When I reload the Django webserver it no anycodings_python longer shows the media.deleted objects.

I have a Django application using Django anycodings_python REST Framework. When I do an Ajax call that anycodings_python changes a model field:

# Delete media
def delete(self, request, pk, format = None):
   media = get_object_or_404(Media, pk = pk)
media.deleted = True
media.save()
return Response(status = status.HTTP_200_OK)

and then trigger a JavaScript anycodings_python location.reload();, the reloaded page still anycodings_python contain the "deleted" model. On the database anycodings_python the deleted field is True, and on my anycodings_python template I make this:

{
   %
   for media in issue.getMedia %
} {
   %
   if not media.deleted %
} {
   % include 'core2/include/media.html' %
} {
   % endif %
} {
   % endfor %
}

The Ajax request is:

controller: function($scope) {
   $scope.execute = function(url) {
      console.log($scope.action)

      $.ajax({
         url: $scope.action,
         type: 'DELETE',
         success: function(result) {
            location.reload();
         },
         error: function(e) {
            alert("Error deleting")
            console.log(e)
         }
      });
   }
}

Try adding the "never_cache" decorator anycodings_javascript to the view reponsible for page anycodings_javascript rendering:

from django.views.decorators.cache
import never_cache

@never_cache
def myview(request, ...):
   ...

or to the dispatch method if it's a CBV:

class MyView(View):

   @never_cache
def dispatch(self, request, * args, ** kwargs):
   return super().dispatch(request, * args, ** kwargs)

Suggestion : 3

By default a ModelForm is dynamically created for your model. It is used to create the form presented on both the add/change pages. You can easily provide your own ModelForm to override any default form behavior on the add/change pages. Alternatively, you can customize the default form rather than specifying an entirely new one by using the ModelAdmin.get_form() method.,Lists of applications and models are sorted alphabetically by their names. You can override this method to change the default order on the admin index page.,It is important you use a ModelForm here otherwise things can break. See the forms documentation on custom validation and, more specifically, the model form validation notes for more information.,You can also add custom validation of data in the admin. The automatic admin interface reuses django.forms, and the ModelAdmin class gives you the ability to define your own form:

from django.contrib
import admin
from myapp.models
import Author

class AuthorAdmin(admin.ModelAdmin):
   pass
admin.site.register(Author, AuthorAdmin)
from django.contrib
import admin
from myapp.models
import Author

admin.site.register(Author)
from django.contrib
import admin
from.models
import Author

@admin.register(Author)
class AuthorAdmin(admin.ModelAdmin):
   pass
from django.contrib
import admin
from.models
import Author, Editor, Reader
from myproject.admin_site
import custom_admin_site

@admin.register(Author, Reader, Editor, site = custom_admin_site)
class PersonAdmin(admin.ModelAdmin):
   pass
from django.contrib
import admin

class AuthorAdmin(admin.ModelAdmin):
   date_hierarchy = 'pub_date'
date_hierarchy = 'pub_date'

Suggestion : 4

Django doesn't update HTML template after `model.save()`,set a variable in django template file, and update it after iteration,How to remove session variable in a template after it's job is done in django,Can I add or update a new variable after render in django template

multiply_add.py

from django
import template
register = template.Library()

@register.simple_tag
def mul_add(a, b, base_value):
   return (a * b) + base_value

template.html

{% load multiply_add %}

{% for adv in advs %}
<div class="media-item big" style="top: 18%;left:{% multiply_add forloop.counter0 774 304 %}px;">
    <div class="media-item__tags">
         <a href="#" class="tag">{{ adv.year }}</a>
         <a href="#" class="tag">{{ adv.payer}}</a>
    </div>
    <div class="media-item__content">
        <div class="media-item__background">
            <a href="project-spar.html" class="media-item-background__link"></a>
            <div class="media-item__canvas">
                <div class="media-item__canvas-background" style="background-image: url({{adv.image.url}})"></div>
            </div>
            <h2 class="topic white upcase fixed-size">{{ adv.name }}</h2>
            <a href="#" class="link regular width600-only">Смотреть проект</a>
        </div>
    </div>
</div>

Suggestion : 5

The field flags can also control how the serializer should be displayed in certain circumstances, such as when rendering to HTML. The {'base_template': 'textarea.html'} flag above is equivalent to using widget=widgets.Textarea on a Django Form class. This is particularly useful for controlling how the browsable API should be displayed, as we'll see later in the tutorial.,A serializer class is very similar to a Django Form class, and includes similar validation flags on the various fields, such as required, max_length and default.,Notice how similar the API is to working with forms. The similarity should become even more apparent when we start writing views that use our serializer.,This tutorial will cover creating a simple pastebin code highlighting Web API. Along the way it will introduce the various components that make up REST framework, and give you a comprehensive understanding of how everything fits together.

Before we do anything else we'll create a new virtual environment, using venv. This will make sure our package configuration is kept nicely isolated from any other projects we're working on.

python3 - m venv env
source env / bin / activate

Now that we're inside a virtual environment, we can install our package requirements.

pip install django
pip install djangorestframework
pip install pygments # We 'll be using this for the code highlighting

Okay, we're ready to get coding. To get started, let's create a new project to work with.

cd~
   django - admin startproject tutorial
cd tutorial

We'll need to add our new snippets app and the rest_framework app to INSTALLED_APPS. Let's edit the tutorial/settings.py file:

INSTALLED_APPS = [
   ...
   'rest_framework',
   'snippets',
]

For the purposes of this tutorial we're going to start by creating a simple Snippet model that is used to store code snippets. Go ahead and edit the snippets/models.py file. Note: Good programming practices include comments. Although you will find them in our repository version of this tutorial code, we have omitted them here to focus on the code itself.

from django.db
import models
from pygments.lexers
import get_all_lexers
from pygments.styles
import get_all_styles

LEXERS = [item
   for item in get_all_lexers() if item[1]
]
LANGUAGE_CHOICES = sorted([(item[1][0], item[0]) for item in LEXERS])
STYLE_CHOICES = sorted([(item, item) for item in get_all_styles()])

class Snippet(models.Model):
   created = models.DateTimeField(auto_now_add = True)
title = models.CharField(max_length = 100, blank = True,
   default = '')
code = models.TextField()
linenos = models.BooleanField(
   default = False)
language = models.CharField(choices = LANGUAGE_CHOICES,
   default = 'python', max_length = 100)
style = models.CharField(choices = STYLE_CHOICES,
   default = 'friendly', max_length = 100)

class Meta:
   ordering = ['created']