我第一次尝试在Django中使用django-国家申请,但是我得到了这个错误,这让我很困惑。
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>这类似于先前的一个问题,但我修正了几个问题,并更新了一些细节。我删除了前面的问题。
发布于 2014-05-18 18:38:22
您的模型和您的视图具有相同的名称,因此您有一个名称空间冲突。更改视图的名称,就可以了。
错误表示您需要传递一个参数,因为您已经将Person重新定义为带有1参数的函数(request)。像这样的东西应该可以工作(调整您的urls.py):
def create_survey(request):
# ...发布于 2014-05-18 18:38:36
你有人的模型类和人的函数。给其中一个命名(函数无论如何不应该以大写字母开头)。
看起来,Person函数需要一个request参数,而不是传入该参数。我想你是想用Person这个类,但是重新定义是令人困惑的事情。
https://stackoverflow.com/questions/23725477
复制相似问题