django-countries: Person() 需要 1 个参数(给了 0 个)
我第一次尝试在Django中使用django-countries应用,但是遇到了一个让我很困惑的错误。
TypeError at /survey/
Person() takes exactly 1 argument (0 given)
我通过pip在项目的虚拟环境中安装了django-countries 2.1.2。
INSTALLED_APPS
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls',
'survey',
'django_countries',
)
我使用的是Django 1.6.4。
models.py
from django.db import models
from django_countries.fields import CountryField
class Person(models.Model):
country = CountryField()
def __unicode__(self):
return self.country
views.py
from django.shortcuts import render
from django.db import models
from django_countries.fields import CountryField
from models import SexChoice, AgeChoice, RelationshipStatusChoice, Person
def Person(request):
age = AgeChoice()
sex = SexChoice()
relationship = RelationshipStatusChoice()
country = Person()
return render(request, 'survey.html', {
'age': age,
'sex': sex,
'relationship': relationship,
'country': country,
})
survy.html
<html>
<body>
<h1>Experiment Survey</h1>
<form action="" method="post">
{% csrf_token %}
<h3>What age are you?</h3>
{{age.as_p}}
<h3>What sex are you?</h3>
{{sex.as_p}}
<h3>What is your current relationship status?</h3>
{{relationship.as_p}}
<h3>What country are you from?</h3>
{{country.as_p}}
<input type="submit" value="Submit" />
</form>
</body>
</html>
附言:这个问题和之前的一个问题类似,不过我修复了一些问题并更新了一些细节。我已经删除了之前的问题。
2 个回答
1
你有一个叫做 Person 的模型类,还有一个叫做 Person 的函数。你可以把其中一个改个名字(而且函数的名字通常不应该以大写字母开头)。
看起来 Person 这个函数需要一个叫做 request
的参数,但你没有传这个参数。我觉得你是想用 Person 这个类,但因为名字重复,搞得有点混乱。
1
你的模型和视图的名字是一样的,这就造成了命名冲突。把视图的名字改一下就没问题了。
错误提示说你需要传一个参数,因为你把Person
重新定义成了一个需要一个参数(request
)的函数。像下面这样应该可以解决问题(调整你的urls.py
文件):
def create_survey(request):
# ...