python - 我正在使用的方法未绑定(bind)?

标签 python django

我正在通过 Python 的 HTMLCalendar 和 Django 制作日历。我用来将数据输入日历的函数显示为未绑定(bind),因此无法工作。

这是日历代码:

from www.wednesday.models import Event
import calendar
e = Event()
class EventCal(calendar.HTMLCalendar):

    def formatday(self, day, weekday):
        if day == 0:
            return '<td class="noday">&nbsp;</td>' # Day outside month
        if day == e.dd():
            return '<td class="%s">%d</p><a href=\"%s\" target=\"_blank\">%s</a></td>' % (self.cssclasses[weekday], day, e.link(), e.res())
        else:
            return '<td class="%s">%d</td>' % (self.cssclasses[weekday], day)

class rendCal:
    c = EventCal(calendar.SUNDAY)

这是我的 models.py:

from django.db import models

class Event(models.Model):
    Restaurant = models.CharField(max_length=200)
    LinkURL = models.CharField(max_length=200)
    created = models.DateTimeField(auto_now_add=True)
    DateDay = models.IntegerField(max_length=2)

    def dd(self):
        return '%i' % self.DateDay

    def link(self):
        return '%s' % self.LinkURL

    def res(self):
        return '%s' % self.Restaurant

最后,我的观点.py:

from django.shortcuts import render_to_response
import www.wednesday.models
from www.wednesday.cal import rendCal
import datetime as dt

def calendar(request):
    now = dt.datetime.now()
    cal = rendCal.c.formatmonth(now.year, now.month)
    return render_to_response('cal.html', {'calendar': cal})

除了在 EventCal 类内部调用的 Event 函数之外,一切正常。

显然我对此很陌生。

好的,@Marcin 询问了一个错误,这就是我所看到的,我也更正了大小写。

TypeError at /calendar/
unbound method dd() must be called with Event instance as first argument (got nothing instead)
cal.py in formatday, line 9

EventCal 中的环境变量显示为空白,我很确定这就是为什么我收到 need int not str 错误的原因。当我将 e.dd() 更改为静态数字时,它返回除 e.link() 和 e.res() 之外的所有内容。

最佳答案

dd() 是类实例的方法。

你可以这样调用它:

e = Event()
x = e.dd()

您无法将 dd 应用于事件本身。

我不确定您到底想做什么,所以我不确定您需要如何修改代码。

关于python - 我正在使用的方法未绑定(bind)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6754192/

相关文章:

python - 数据库更改的更新页面

django - 返回 DRF 序列化程序中选择字段的人类可读元素

python - 通过单击另一个复选框禁用复选框 (HTML/DJANGO)

python - NLTK 每个词最常见的同义词(Wordnet)

python - 检测语言和 django locale-url

python - Python 打包状态 : Buildout, Distribute、Distutils、EasyInstall 等

python - 如何遍历文件中的每第 n 行?

python - capitalize() 方法如何在 python/django 中工作?

python - 如何在Python中使用正则表达式向下面的行添加一个单词?

python - 为什么 for 循环中的 del list[0] 只删除列表的一半?