Reputation: 18725
I want to create a form which allows user
(extended OneToOne by UserProfile
) to became a translator
. This means that if the User
is logged-in, he/she can click on Become a Translator
button which redirects them to pre-filled form. This form should be pre-filled if there is not null such attribute in UserProfile
.
So if somebody has already filled his last_name, the last name should be prefilled in this form, otherwise, he has to fill it because for Translator it's required.
So I've tried to put user.userprofile argument as an attribute of the form class but it raises:
'UserProfile' object has no attribute 'get'
Could you give me some hint?
@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)
EDIT: 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'
Upvotes: 0
Views: 523
Reputation: 8250
You are passing request.user.userprofile
as a positional argument to TranslatorRegistrationForm
in this line:
register_as_translator_form = TranslatorRegistrationForm(request.user.userprofile)
You should pass it as a keyword argument because Django forms expect data
(POST/GET) as the first positional argument.
If your form accepts userprofile keyword argument in the __init__
method, you should pass it as a keyword argument instead. Like this:
register_as_translator_form = TranslatorRegistrationForm(user=request.user.userprofile)
If you want to pass initial data to the form, you can pass it using initial
keyword argument instead.
initial_data = {'name': 'John'}
register_as_translator_form = TranslatorRegistrationForm(initial=initial_data)
Note: Initial data keys must match your form fields.
Upvotes: 2