deathstroke
deathstroke

Reputation: 526

Django-changes made to admin are not reflected in the template without restarting the server

The changes I'm making to the database from my admin interface are not reflected unless I restart the server.

Here's my models.py file-

class InventoryManager(models.Manager):

    def getActiveId(self):
        return self.filter(currentlyActive=True)
    ....
    ....

class Inventory(models.Model):
    ....
    ....
    ....

    name = models.CharField(_("item name"),max_length=30)
    category = models.CharField(_("breakfast/lunch/dinner"), max_length=2,choices=TIME_OF_SERVICE, default="lunch")
    foodType = models.CharField(_("veg/non-veg"), max_length=7,choices=TYPE_OF_FOOD, default="veg")
    stock = models.IntegerField(_("stock quantity"))
    image = models.ImageField(_("food item photo"),upload_to='images/',null=True)
    currentlyActive = models.BooleanField(_("active/inactive for the day"),default=0)
    description = models.CharField(_("food item description"),max_length=150)
    objects = InventoryManager()

    def __str__(self):
        return self.name
....
....

Here, I'm filtering out those entries in the database that have currentlyActive as True.

Now, in my views.py file:

def getActiveItems():
    idList = []
    query = Inventory.objects.getActiveId().values()
    for i in query:
        idList.append(i['id'])
    return idList

def menu(request,Id):
    photos = []
    menu = []
    for i in xrange(len(Id)):
        photos.append(str(Inventory.objects.values().get(id=Id[i])['image']))
        menu.append(str(Inventory.objects.values().get(id=Id[i])['name']))
    print menu
    context={'imageList': zip(photos,menu)}
    return render(request,'menu.html', context)

The urls.py file-

....
....
urlpatterns = patterns('',
    url(r'^$', menu, {'Id' : getActiveItems()}),
    url(r'^admin/', include(admin.site.urls)),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

And finally, the menu.html template file-

....
....
{% block content %}

{% for image,menu in imageList %}
<div style = "display:block">
    <img src="{{ MEDIA_URL }}{{ image }}">
    <p>{{ menu }}</p>
</div>
{% endfor %}
{% endblock %}

Now, in the menu function, I have used the print menu statement that prints those items from the database that are currentlyActive.

When I change the currentlyActive boolean in the database and then load the page, the print menu shows me the correct values. (for example, print menu shows me [2,3,4] when the 2nd, 3rd and 4th entries in the db have currentlyActive set as True. When I set the 5th entry's currentlyActive as True and then load the main page, it does show me [2,3,4,5]).

So, I figured out the problem lay while rendering the template or while calling the menu function.

When the kill the local server and restart it again, the changes are reflected in the rendered template perfectly.

Can't figure out why this is happening.

Upvotes: 2

Views: 1090

Answers (1)

Emma
Emma

Reputation: 1081

The problem is neither while rendering the template or while calling the function, the problem is in the url definition.

urls.py is only evaluated once when the server is first started. So when you write:

url(r'^$', menu, {'Id' : getActiveItems()}),

getActiveItems is evaluated then and never gets called again (until you restart the server).

Instead of calling getActiveItems from your urls.py and passing its result to the menu view, you should call it from inside your view.

urls.py

....
urlpatterns = patterns('',
    url(r'^$', menu),
    url(r'^admin/', include(admin.site.urls)),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

views.py

...
def menu(request):
    Id = getActiveItems()
    photos = []
    menu = []
    ...

If you are planning on calling the view with the Id parameter from somewhere else, make it non-mandatory and only call getActiveItems if the parameter is null

Upvotes: 2

Related Questions