我一直在关注Django的轻鸟初学者教程。 link
在第1部分教程的最后一部分,我们的任务是开发一个进度条。我无法使用此进度条来使用以下代码。
from django.db import models
from django.contrib import admin
from django.utils.translation import ugettext as _
from django.utils.encoding import force_unicode
from django.http import HttpResponse, HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
# Create your 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 the HttpResponse for the add_view stage. """
for 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) were added successfully."
# Here, we distinguish between different save types by checking for
# the presence of keys in request.POST.
if request.POST.has_key("_continue"):
self.message_user(request, msg + ' ' + _("You may edit it 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 another %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)
我的项目页面上没有任何进度条,就像教程中的截图一样。
答案 0 :(得分:0)
答案非常简单,完全没有在教程中解决。在ItemAdmin类中,您需要为教程中定义的"progress_"
函数添加list_display。
from django.db import models
from django.contrib import admin
from django.utils.translation import ugettext as _
from django.utils.encoding import force_unicode
from django.http import HttpResponse, HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
# Create your 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 the HttpResponse for the add_view stage. """
for 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) were added successfully."
# Here, we distinguish between different save types by checking for
# the presence of keys in request.POST.
if request.POST.has_key("_continue"):
self.message_user(request, msg + ' ' + _("You may edit it 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 another %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)