django表单的关键字参数有多个值 - python

我有一个简单的模型,如下所示:

RATING_CHOICES = zip(range(1, 6), range(1, 6))
class Rating(models.Model):

    value = models.IntegerField(choices=RATING_CHOICES)
    additional_note = models.TextField(null=True, blank=True)
    from_user = models.ForeignKey(User, related_name='from_user')
    to_user = models.ForeignKey(User, related_name='to_user')
    shared_object = models.ForeignKey(ObjectDetail, null=True, blank=True)
    dtobject = models.DateTimeField(auto_now_add=True)

从上面的模型中,我在forms.py中生成一个模型表单,如下所示:

class RatingForm(ModelForm):

     class Meta:
          model = Rating
          exclude = ('from_user', 'dtobject',
                     'shared_object')

在我的网址中,尝试以下操作:

url(r'^rate/(?P<form_type>[\w]+)/(?P<oid>\d+)/(?P<oslug>[\w-]+)/$', 'rating_form', name='rating_form'),                     

在我看来,以下内容:

def rating_form(form_type = None, oid = None, oslug=None):

    print form_type
    form = RatingForm(data=request.POST or None)

    if request.POST and form.is_valid():
           form.save()
        return HttpResponseRedirect("/")
    else:
        return render(request, "share.html", {'form' : form })

这样做给我以下错误:

rating_form()为关键字参数“form_type”获得了多个值

额外细节:

Request Method: GET
Request URL:    http://127.0.0.1:8000/rate/lending/3/random-stuff/
Django Version: 1.4.1
Exception Type: TypeError
Exception Value:    
rating_form() got multiple values for keyword argument 'form_type'
Exception Location: /Library/Python/2.7/site-packages/django/contrib/auth/decorators.py in _wrapped_view, line 20
Python Executable:  /usr/bin/python

我究竟做错了什么?

参考方案

您视图的第一个参数应为request

django-simple-history,在admin中显示更改的字段 - python

当我从admin.ModelAdmin继承时,在管理页面的历史记录中,我可以看到哪些字段已更改。但是,现在我需要使用django-simple-history来跟踪所有模型更改。现在,对于管理员,我继承了simple_history.SimpleHistoryAdmin。我可以看到所有模型更改并还原它们,但看不到更改了哪些字段。是否可以在SimpleHist…

Django Python如何在给定两个时间字符串的情况下计算时差 - python

在Django Python中,我有2个HH:mm时间字符串,如何获取持续时间(差异)?例如:15:30 and 11:00 ---> difference is 04:30 19:28 and 12:25 ---> difference is 07:03 参考方案 我们可以首先使用以下命令将字符串解析为datetime对象:from datet…

Django Python日期时间设置为午夜 - python

我有django对象的日期时间,但可以是一天中的任何时间。它可以是一天中的任何时间,但我需要将时间设置为00:00:00(另一个日期设置为23:59:59,但原理是相同的)end_date = lastItem.pub_date 当前的结束日期是2002-01-11 12:34:56我需要怎么做才能将其更改为00:00:00?我试过了:end_date.ho…

如何从Django模型生成文档? - python

目前,我们将Sphinx用于项目文档和Django模型字段描述。主要问题是:更改模型后,我们手动更新了Sphinx文档,有时会忘记/错过文档中的某些字段。有一些用于基于Django模型生成文档的工具吗? 参考方案 documentation说: Django的文件使用Sphinx文件系统, 转是基于docutils的。基本思想是格式化 纯文本文档已转换为HT…

django-compressor未与django-shop一起安装 - python

我无法使用django-shop安装django-compressor。出现这样的错误。Failed building wheel for rcssmin ================================= Failed building wheel for rjsmin -----------------------------------…