Django - ' UserProfile'对象没有属性' get'

时间:2016-04-01 13:55:08

标签: python django django-models django-forms

我想创建一个允许userUserProfile扩展的OneToOne)成为translator的表单。这意味着如果User已登录,他/她可以点击Become a Translator按钮,将其重定向到预先填写的表单。如果UserProfile中没有此类属性,则应预先填写此表单。

因此,如果有人已经填写了他的姓氏,那么姓氏应该以这种形式预先填写,否则,他必须填写它,因为对于译者来说,这是必需的。

所以我试图将user.userprofile参数作为表单类的属性,但它引发了:

 'UserProfile' object has no attribute 'get'

你能给我一些提示吗?

@login_required
def register_as_translator(request):
    register_as_translator_form = TranslatorRegistrationForm(request.POST or None)
    if request.method == 'POST':
        if register_as_translator_form.is_valid():
            pass
    register_as_translator_form = TranslatorRegistrationForm(request.user.userprofile)
    context = {
        'register_as_translator_form':register_as_translator_form,
    }
    return render(request,'auth/registration/register-translator.html',context=context)


class UserProfile(models.Model):
    user = models.OneToOneField(User, related_name='userprofile')
    date_of_birth = models.DateField(null=True,blank=True)
    telephone = models.CharField(max_length=40,null=True,blank=True)
    IBAN = models.CharField(max_length=40,null=True,blank=True)
    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)


    MARITAL_STATUS_CHOICES = (
        ('single', 'Single'),
        ('married', 'Married'),
        ('separated', 'Separated'),
        ('divorced', 'Divorced'),
        ('widowed', 'Widowed'),
    )
    marital_status = models.CharField(max_length=40, choices=MARITAL_STATUS_CHOICES, null=True, blank=True)

    HOW_DO_YOU_KNOW_ABOUT_US_CHOICES = (
        ('coincidence', u'It was coincidence'),
        ('relative_or_friends', 'From my relatives or friends'),
    )
    how_do_you_know_about_us = models.CharField(max_length=40, choices=HOW_DO_YOU_KNOW_ABOUT_US_CHOICES, null=True,
                                                blank=True)

    # TRANSLATOR ATTRIBUTES

    is_translator = models.BooleanField(default=False)

    language_tuples = models.ManyToManyField(LanguageTuple)

    rating = models.IntegerField(default=0)

    number_of_ratings = models.BigIntegerField(default=0)

    def __unicode__(self):
        return '{} {}'.format(self.user.first_name, self.user.last_name)

    def __str__(self):
        return '{} {}'.format(self.user.first_name, self.user.last_name)

编辑: TRACEBACK:

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/register-as-translator

Django Version: 1.9.4
Python Version: 2.7.10
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'SolutionsForLanguagesApp',
 'crispy_forms')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware')


Template error:
In template C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\crispy_forms\templates\bootstrap\uni_form.html, error at line 1
   'UserProfile' object has no attribute 'get'   1 :  {% load crispy_forms_utils %}
    2 : 
   3 : {% specialspaceless %}
   4 :     {% if include_media %}{{ form.media }}{% endif %}
   5 :     {% if form_show_errors %}
   6 :         {% include "bootstrap/errors.html" %}
   7 :     {% endif %}
   8 :     {% for field in form %}
   9 :         {% include "bootstrap/field.html" %}
   10 :     {% endfor %}
   11 : {% endspecialspaceless %}


Traceback:

File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\core\handlers\base.py" in get_response
  149.                     response = self.process_exception_by_middleware(e, request)

File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\core\handlers\base.py" in get_response
  147.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\contrib\auth\decorators.py" in _wrapped_view
  23.                 return view_func(request, *args, **kwargs)

File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\SolutionsForLanguagesApp\views.py" in register_as_translator
  32.     return render(request,'auth/registration/register-translator.html',context=context)

File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\shortcuts.py" in render
  67.             template_name, context, request=request, using=using)

File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\loader.py" in render_to_string
  97.         return template.render(context, request)

File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\backends\django.py" in render
  95.             return self.template.render(context)

File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\base.py" in render
  206.                     return self._render(context)

File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\base.py" in _render
  197.         return self.nodelist.render(context)

File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\base.py" in render
  992.                 bit = node.render_annotated(context)

File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\base.py" in render_annotated
  959.             return self.render(context)

File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\loader_tags.py" in render
  173.         return compiled_parent._render(context)

File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\base.py" in _render
  197.         return self.nodelist.render(context)

File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\base.py" in render
  992.                 bit = node.render_annotated(context)

File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\base.py" in render_annotated
  959.             return self.render(context)

File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\loader_tags.py" in render
  69.                 result = block.nodelist.render(context)

File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\base.py" in render
  992.                 bit = node.render_annotated(context)

File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\base.py" in render_annotated
  959.             return self.render(context)

File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\base.py" in render
  1043.             output = self.filter_expression.resolve(context)

File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\base.py" in resolve
  736.                 new_obj = func(obj, *arg_vals)

File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\crispy_forms\templatetags\crispy_forms_filters.py" in as_crispy_form
  70.     return template.render(c)

File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\backends\django.py" in render
  95.             return self.template.render(context)

File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\base.py" in render
  206.                     return self._render(context)

File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\base.py" in _render
  197.         return self.nodelist.render(context)

File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\base.py" in render
  992.                 bit = node.render_annotated(context)

File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\base.py" in render_annotated
  959.             return self.render(context)

File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\crispy_forms\templatetags\crispy_forms_utils.py" in render
  27.         return remove_spaces(self.nodelist.render(context).strip())

File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\base.py" in render
  992.                 bit = node.render_annotated(context)

File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\base.py" in render_annotated
  959.             return self.render(context)

File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\defaulttags.py" in render
  326.                 return nodelist.render(context)

File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\base.py" in render
  992.                 bit = node.render_annotated(context)

File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\base.py" in render_annotated
  959.             return self.render(context)

File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\loader_tags.py" in render
  209.                 return template.render(context)

File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\base.py" in render
  208.                 return self._render(context)

File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\base.py" in _render
  197.         return self.nodelist.render(context)

File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\base.py" in render
  992.                 bit = node.render_annotated(context)

File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\base.py" in render_annotated
  959.             return self.render(context)

File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\defaulttags.py" in render
  319.                     match = condition.eval(context)

File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\defaulttags.py" in eval
  951.         return self.value.resolve(context, ignore_failures=True)

File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\base.py" in resolve
  709.                 obj = self.var.resolve(context)

File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\base.py" in resolve
  850.             value = self._resolve_lookup(context)

File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\base.py" in _resolve_lookup
  913.                             current = current()

File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\forms\forms.py" in non_field_errors
  289.         return self.errors.get(NON_FIELD_ERRORS, self.error_class(error_class='nonfield'))

File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\forms\forms.py" in errors
  153.             self.full_clean()

File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\forms\forms.py" in full_clean
  362.         self._clean_fields()

File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\forms\forms.py" in _clean_fields
  374.                 value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name))

File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\forms\widgets.py" in value_from_datadict
  231.         return data.get(name)

Exception Type: AttributeError at /register-as-translator
Exception Value: 'UserProfile' object has no attribute 'get'

1 个答案:

答案 0 :(得分:2)

您将request.user.userprofile作为TranslatorRegistrationForm的位置参数传递到此行:

register_as_translator_form = TranslatorRegistrationForm(request.user.userprofile)

你应该把它作为关键字参数传递,因为Django表格期望data(POST / GET)作为第一个位置参数。

如果您的表单在__init__方法中接受userprofile关键字参数,则应将其作为关键字参数传递。像这样:

register_as_translator_form = TranslatorRegistrationForm(user=request.user.userprofile)

如果要将初始数据传递给表单,可以使用initial关键字参数传递它。

initial_data = {'name': 'John'}
register_as_translator_form = TranslatorRegistrationForm(initial=initial_data)

注意:初始数据密钥必须与您的表单字段匹配。