python - Progress Bar Todo List Tutorial -
i have been following light bird beginner's tutorial django. link
in last part of part 1 tutorial tasked developing progress bar. not able progress bar work following code.
from django.db import models django.contrib import admin django.utils.translation import ugettext _ django.utils.encoding import force_unicode django.http import httpresponse, httpresponseredirect django.core.urlresolvers import reverse django.contrib.auth.models import user # create models here. class datetime(models.model): datetime = models.datetimefield(auto_now_add=true) def __unicode__(self): return unicode(self.datetime.strftime("%b %d, %y, %i:%m %p")) class item(models.model): name = models.charfield(max_length=60) progress = models.integerfield(default=0) created = models.foreignkey(datetime) priority = models.integerfield(default=0) difficulty = models.integerfield(default=0) done = models.booleanfield(default=false) user = models.foreignkey(user, blank=true, null=true) def mark_done(self): return "<a href='%s'>done</a>" % reverse("todo.views.mark_done", args=[self.pk]) mark_done.allow_tags = true def progress_(self): return "<div style='width: 100px; border: 1px solid #ccc;'>" + \ "<div style='height: 4px; width: %dpx; background: #555; '></div></div>" % self.progress progress_.allow_tags = true class itemadmin(admin.modeladmin): list_display = ["name", "priority", "difficulty", "created", "done", "user"] search_fields = ["name"] class iteminline(admin.tabularinline): model = item class dateadmin(admin.modeladmin): list_display = ["datetime"] inlines = [iteminline] def response_add(self, request, obj, post_url_continue='../%s/'): """ determines httpresponse add_view stage. """ item in item.objects.filter(created=obj): if not item.user: item.user = request.user item.save() return httpresponseredirect(reverse("admin:todo_item_changelist")) opts = obj._meta pk_value = obj._get_pk_val() msg = "item(s) added successfully." # here, distinguish between different save types checking # presence of keys in request.post. if request.post.has_key("_continue"): self.message_user(request, msg + ' ' + _("you may edit again below.")) if request.post.has_key("_popup"): post_url_continue += "?_popup=1" return httpresponseredirect(post_url_continue % pk_value) if request.post.has_key("_popup"): return httpresponse( '<script type="text/javascript">opener.dismissaddanotherpopup(window, "%s", "%s");' '</script>' % (escape(pk_value), escape(obj))) elif request.post.has_key("_addanother"): self.message_user(request, msg + ' ' + (_("you may add %s below.") % force_unicode(opts.verbose_name))) return httpresponseredirect(request.path) else: self.message_user(request, msg) return httpresponseredirect(reverse("admin:todo_item_changelist")) admin.site.register(item, itemadmin) admin.site.register(datetime, dateadmin)
i not have progress bar on items page @ screenshot tutorial.
the answer simple , not addressed in tutorial @ all. in itemadmin class need add list_display "progress_"
function defined in tutorial.
from django.db import models django.contrib import admin django.utils.translation import ugettext _ django.utils.encoding import force_unicode django.http import httpresponse, httpresponseredirect django.core.urlresolvers import reverse django.contrib.auth.models import user # create models here. class datetime(models.model): datetime = models.datetimefield(auto_now_add=true) def __unicode__(self): return unicode(self.datetime.strftime("%b %d, %y, %i:%m %p")) class item(models.model): name = models.charfield(max_length=60) progress = models.integerfield(default=0) created = models.foreignkey(datetime) priority = models.integerfield(default=0) difficulty = models.integerfield(default=0) done = models.booleanfield(default=false) user = models.foreignkey(user, blank=true, null=true) def mark_done(self): return "<a href='%s'>done</a>" % reverse("todo.views.mark_done", args=[self.pk]) mark_done.allow_tags = true def progress_(self): return "<div style='width: 100px; border: 1px solid #ccc;'>" + \ "<div style='height: 4px; width: %dpx; background: #555; '></div></div>" % self.progress progress_.allow_tags = true class itemadmin(admin.modeladmin): list_display = ["name", "priority", "progress_", "difficulty", "created", "done", "user"] search_fields = ["name"] class iteminline(admin.tabularinline): model = item class dateadmin(admin.modeladmin): list_display = ["datetime"] inlines = [iteminline] def response_add(self, request, obj, post_url_continue='../%s/'): """ determines httpresponse add_view stage. """ item in item.objects.filter(created=obj): if not item.user: item.user = request.user item.save() return httpresponseredirect(reverse("admin:todo_item_changelist")) opts = obj._meta pk_value = obj._get_pk_val() msg = "item(s) added successfully." # here, distinguish between different save types checking # presence of keys in request.post. if request.post.has_key("_continue"): self.message_user(request, msg + ' ' + _("you may edit again below.")) if request.post.has_key("_popup"): post_url_continue += "?_popup=1" return httpresponseredirect(post_url_continue % pk_value) if request.post.has_key("_popup"): return httpresponse( '<script type="text/javascript">opener.dismissaddanotherpopup(window, "%s", "%s");' '</script>' % (escape(pk_value), escape(obj))) elif request.post.has_key("_addanother"): self.message_user(request, msg + ' ' + (_("you may add %s below.") % force_unicode(opts.verbose_name))) return httpresponseredirect(request.path) else: self.message_user(request, msg) return httpresponseredirect(reverse("admin:todo_item_changelist")) admin.site.register(item, itemadmin) admin.site.register(datetime, dateadmin)
Comments
Post a Comment