django-countries: Person()只接受1个参数(给定0)



我第一次尝试使用Django - nations应用程序,但我得到了这个错误,这让我感到困惑。

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>

注:这类似于之前的问题,但我修复了一些问题,并更新了一些细节。我删除了之前的问题。

您的模型您的视图具有相同的名称,因此您有名称空间冲突。更改视图的名称就可以了。

错误提示您需要传递一个参数,因为您已将Person重新定义为具有1个参数的函数(request)。像这样的东西应该工作(调整您的urls.py):

def create_survey(request): 
     # ...

Person是模型类,Person是函数。将它们中的一个命名为其他名称(函数不应该以大写字母开头)。

看起来像Person函数需要一个request参数,您没有传递进去。我想你的意思是使用Person类,但是重新定义是混淆的事情。

最新更新