django-countries: Person() takes exactly 1 argument (0 given)

匿名 (未验证) 提交于 2019-12-03 02:38:01

问题:

I am trying to use the django-countries application with Django for the first time but I am getting this error which has me confused.

TypeError at /survey/  Person() takes exactly 1 argument (0 given) 

I installed django-countries 2.1.2 via pip in a virtual environment for the project.

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', ) 

I am using 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> 

P.S. This is similar to an earlier question but I fixed a few issues and updated some details. I deleted the earlier question.

回答1:

Your model and your view has the same name so you have a namespace conflict. Change the name of the view and it will be fine.

The error says you need to pass an argument because you have redefined Person to be a function with 1 argument (request). Something like this should work (adapt your urls.py):

def create_survey(request):       # ... 


回答2:

You have Person the model class and Person the function. Name one of them something else (and functions should not start with capitals anyway).

Looks like Person the function requires a request parameter, which you're not passing in. I think you mean to be using Person the class, but the redefinition is confusing things.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!