Pass parameter to django form - django

I need to dynamically create a choice field in Django Form (not ModelForm) using the logged user as a parameter.
The view:
def cadastro(request):
medico = request.user.medico
usuario=request.user
clinicas = medicos.clinicas.all()
escolhas = tuple([(c.id, c.nome.clinica) for c in clinicas])
if request.method == 'POST':
formulario = NovoProcesso(escolhas, request.POST)
if formulario.is_valid():
formulario.save(usuario)
else:
formulario = NovoProcesso(escolhas)
contexto = {'formulario': formulario, 'clinicas': clinicas}
return render(request, 'processos/cadastro.html', contexto)
Form class:
class NovoProcesso(forms.Form):
def __init__(self, escolhas, *args, **kwargs):
super(NovoProcesso, self).__init__(escolhas, *args, **kwargs) -------> the source of the problem is here.
self.fields['clinicas'].choices = escolhas
clinicas = forms.ChoiceField(widget=forms.Select, choices=[])
# there are other fields here, but I ommited for the sake of clarity
def(save):
#custom save method....
I incorrectly passed arguments to the init method as described here:
object has no attribute 'get'
I'm having trouble to understand the line:
self.fields['clinicas'].choices = escolhas
Not sure if I should write self.fields['clinicas].choices or . widget.... not sure how to write de form field inside the class. Tried several variations.
I'm getting the following error:
Internal Server Error: /processos/cadastro/ Traceback (most recent
call last): File
"/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/forms/forms.py",
line 158, in getitem
field = self.fields[name] KeyError: 'errors'
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File
"/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/template/base.py",
line 828, in _resolve_lookup
current = current[bit] File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/forms/forms.py",
line 160, in getitem
raise KeyError( KeyError: "Key 'errors' not found in 'NovoProcesso'. Choices are: altura, anamnese, cid, clinicas,
cpf_paciente, data_1, diagnostico, incapaz, med1, med1_posologia_mes1,
nome_mae, nome_paciente, nome_responsavel, peso, qtd_med1_mes1,
qtd_med1_mes2, qtd_med1_mes3, tratamentos_previos, tratou."
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File
"/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/core/handlers/exception.py",
line 34, in inner
response = get_response(request) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/core/handlers/base.py",
line 115, in _get_response
response = self.process_exception_by_middleware(e, request) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/core/handlers/base.py",
line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/contrib/auth/decorators.py",
line 21, in _wrapped_view
return view_func(request, *args, **kwargs) File "/home/lucas/dev/autocusto/processos/views.py", line 101, in cadastro
return render(request, 'processos/cadastro.html', contexto) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/shortcuts.py",
line 19, in render
content = loader.render_to_string(template_name, context, request, using=using) File
"/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/template/loader.py",
line 62, in render_to_string
return template.render(context, request) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/template/backends/django.py",
line 61, in render
return self.template.render(context) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/template/base.py",
line 171, in render
return self._render(context) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/template/base.py",
line 163, in _render
return self.nodelist.render(context) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/template/base.py",
line 936, in render
bit = node.render_annotated(context) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/template/base.py",
line 903, in render_annotated
return self.render(context) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/template/loader_tags.py",
line 150, in render
return compiled_parent._render(context) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/template/base.py",
line 163, in _render
return self.nodelist.render(context) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/template/base.py",
line 936, in render
bit = node.render_annotated(context) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/template/base.py",
line 903, in render_annotated
return self.render(context) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/template/loader_tags.py",
line 62, in render
result = block.nodelist.render(context) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/template/base.py",
line 936, in render
bit = node.render_annotated(context) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/template/base.py",
line 903, in render_annotated
return self.render(context) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/template/base.py",
line 986, in render
output = self.filter_expression.resolve(context) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/template/base.py",
line 670, in resolve
obj = self.var.resolve(context) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/template/base.py",
line 795, in resolve
value = self._resolve_lookup(context) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/template/base.py",
line 836, in _resolve_lookup
current = getattr(current, bit) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/forms/forms.py",
line 175, in errors
self.full_clean() File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/forms/forms.py",
line 376, in full_clean
self._clean_fields() File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/forms/forms.py",
line 388, in _clean_fields
value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name)) File
"/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/forms/widgets.py",
line 258, in value_from_datadict
return data.get(name) AttributeError: 'tuple' object has no attribute 'get' [20/Dec/2019 13:09:03] "GET /processos/cadastro/
HTTP/1.1" 500 215106 /home/lucas/dev/autocusto/processos/views.py
changed, reloading. Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced). December 20, 2019 -
13:11:10 Django version 3.0, using settings 'autocusto.settings'
Starting development server at http://127.0.0.1:8000/ Quit the server
with CONTROL-C. /home/lucas/dev/autocusto/processos/forms.py changed,
reloading. Watching for file changes with StatReloader Performing
system checks...
System check identified no issues (0 silenced). December 20, 2019 -
13:11:24 Django version 3.0, using settings 'autocusto.settings'
Starting development server at http://127.0.0.1:8000/ Quit the server
with CONTROL-C. [(1, 'SHAM'), (2, '65'), (3, '65asdfasdf'), (4,
'Clínica 1'), (5, 'Clínica 2'), (6, 'Clínica 3')] Internal Server
Error: /processos/cadastro/ Traceback (most recent call last): File
"/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/forms/forms.py",
line 158, in getitem
field = self.fields[name] KeyError: 'errors'
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File
"/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/template/base.py",
line 828, in _resolve_lookup
current = current[bit] File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/forms/forms.py",
line 160, in getitem
raise KeyError( KeyError: "Key 'errors' not found in 'NovoProcesso'. Choices are: altura, anamnese, cid, clinicas,
cpf_paciente, data_1, diagnostico, incapaz, med1, med1_posologia_mes1,
nome_mae, nome_paciente, nome_responsavel, peso, qtd_med1_mes1,
qtd_med1_mes2, qtd_med1_mes3, tratamentos_previos, tratou."
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File
"/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/core/handlers/exception.py",
line 34, in inner
response = get_response(request) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/core/handlers/base.py",
line 115, in _get_response
response = self.process_exception_by_middleware(e, request) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/core/handlers/base.py",
line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/contrib/auth/decorators.py",
line 21, in _wrapped_view
return view_func(request, *args, **kwargs) File "/home/lucas/dev/autocusto/processos/views.py", line 101, in cadastro
return render(request, 'processos/cadastro.html', contexto) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/shortcuts.py",
line 19, in render
content = loader.render_to_string(template_name, context, request, using=using) File
"/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/template/loader.py",
line 62, in render_to_string
return template.render(context, request) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/template/backends/django.py",
line 61, in render
return self.template.render(context) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/template/base.py",
line 171, in render
return self._render(context) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/template/base.py",
line 163, in _render
return self.nodelist.render(context) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/template/base.py",
line 936, in render
bit = node.render_annotated(context) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/template/base.py",
line 903, in render_annotated
return self.render(context) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/template/loader_tags.py",
line 150, in render
return compiled_parent._render(context) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/template/base.py",
line 163, in _render
return self.nodelist.render(context) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/template/base.py",
line 936, in render
bit = node.render_annotated(context) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/template/base.py",
line 903, in render_annotated
return self.render(context) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/template/loader_tags.py",
line 62, in render
result = block.nodelist.render(context) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/template/base.py",
line 936, in render
bit = node.render_annotated(context) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/template/base.py",
line 903, in render_annotated
return self.render(context) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/template/base.py",
line 986, in render
output = self.filter_expression.resolve(context) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/template/base.py",
line 670, in resolve
obj = self.var.resolve(context) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/template/base.py",
line 795, in resolve
value = self._resolve_lookup(context) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/template/base.py",
line 836, in _resolve_lookup
current = getattr(current, bit) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/forms/forms.py",
line 175, in errors
self.full_clean() File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/forms/forms.py",
line 376, in full_clean
self._clean_fields() File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/forms/forms.py",
line 388, in _clean_fields
value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name)) File
"/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/forms/widgets.py",
line 258, in value_from_datadict
return data.get(name) AttributeError: 'list' object has no attribute 'get' [20/Dec/2019 13:11:28] "GET /processos/cadastro/
HTTP/1.1" 500 215100 /home/lucas/dev/autocusto/processos/forms.py
changed, reloading. Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced). December 20, 2019 -
13:13:15 Django version 3.0, using settings 'autocusto.settings'
Starting development server at http://127.0.0.1:8000/ Quit the server
with CONTROL-C. /home/lucas/dev/autocusto/processos/views.py changed,
reloading. Watching for file changes with StatReloader Performing
system checks...
System check identified no issues (0 silenced). December 20, 2019 -
13:25:04 Django version 3.0, using settings 'autocusto.settings'
Starting development server at http://127.0.0.1:8000/ Quit the server
with CONTROL-C. [20/Dec/2019 13:30:03] "GET / HTTP/1.1" 200 3680 Not
Found: /favicon.ico ((1, 'SHAM'), (2, '65'), (3, '65asdfasdf'), (4,
'Clínica 1'), (5, 'Clínica 2'), (6, 'Clínica 3')) Internal Server
Error: /processos/cadastro/ Traceback (most recent call last): File
"/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/forms/forms.py",
line 158, in getitem
field = self.fields[name] KeyError: 'errors'
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File
"/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/template/base.py",
line 828, in _resolve_lookup
current = current[bit] File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/forms/forms.py",
line 160, in getitem
raise KeyError( KeyError: "Key 'errors' not found in 'NovoProcesso'. Choices are: altura, anamnese, cid, clinicas,
cpf_paciente, data_1, diagnostico, incapaz, med1, med1_posologia_mes1,
nome_mae, nome_paciente, nome_responsavel, peso, qtd_med1_mes1,
qtd_med1_mes2, qtd_med1_mes3, tratamentos_previos, tratou."
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File
"/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/core/handlers/exception.py",
line 34, in inner
response = get_response(request) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/core/handlers/base.py",
line 115, in _get_response
response = self.process_exception_by_middleware(e, request) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/core/handlers/base.py",
line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/contrib/auth/decorators.py",
line 21, in _wrapped_view
return view_func(request, *args, **kwargs) File "/home/lucas/dev/autocusto/processos/views.py", line 101, in cadastro
return render(request, 'processos/cadastro.html', contexto) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/shortcuts.py",
line 19, in render
content = loader.render_to_string(template_name, context, request, using=using) File
"/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/template/loader.py",
line 62, in render_to_string
return template.render(context, request) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/template/backends/django.py",
line 61, in render
return self.template.render(context) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/template/base.py",
line 171, in render
return self._render(context) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/template/base.py",
line 163, in _render
return self.nodelist.render(context) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/template/base.py",
line 936, in render
bit = node.render_annotated(context) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/template/base.py",
line 903, in render_annotated
return self.render(context) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/template/loader_tags.py",
line 150, in render
return compiled_parent._render(context) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/template/base.py",
line 163, in _render
return self.nodelist.render(context) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/template/base.py",
line 936, in render
bit = node.render_annotated(context) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/template/base.py",
line 903, in render_annotated
return self.render(context) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/template/loader_tags.py",
line 62, in render
result = block.nodelist.render(context) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/template/base.py",
line 936, in render
bit = node.render_annotated(context) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/template/base.py",
line 903, in render_annotated
return self.render(context) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/template/base.py",
line 986, in render
output = self.filter_expression.resolve(context) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/template/base.py",
line 670, in resolve
obj = self.var.resolve(context) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/template/base.py",
line 795, in resolve
value = self._resolve_lookup(context) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/template/base.py",
line 836, in _resolve_lookup
current = getattr(current, bit) File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/forms/forms.py",
line 175, in errors
self.full_clean() File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/forms/forms.py",
line 376, in full_clean
self._clean_fields() File "/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/forms/forms.py",
line 388, in _clean_fields
value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name)) File
"/home/lucas/dev/.virtualenvs/autocusto/lib/python3.8/site-packages/django/forms/widgets.py",
line 258, in value_from_datadict
return data.get(name) AttributeError: 'tuple' object has no attribute 'get'
I've tried the following solutions with no success:
Django: how to pass parameters to forms,
Django Forms: pass parameter to form
Don't know what I'm missing!

Probably, in NovoProcesso form class or somewhere else you have "," at the end of one of the attribute definition which causes this error. Try to find it out.
You haven't posted whole code but I guess you have something like this:
class NovoProcesso(forms.Form):
clinicas = forms.ChoiceField(widget=forms.Select, choices=[]), <- this comma causes an error
EDIT: Incorrectly passed arguments to the init method as described here object has no attribute 'get'

Related

Django catch missing manifest exceptions

Every now and then I end up with a missing file after deploy a django project, and I find it one of the hardest issues to debug because I get no logging and no error email, its just dead. I have made a custom 500 error page (followed this answer). Is it possible to show the details of which static file is missing on it? With this error Django fails before rendering a 500 page.
Heres a traceback of the error:
Traceback (most recent call last):
File "/usr/lib/python3.8/wsgiref/handlers.py", line 137, in run
self.result = application(self.environ, self.start_response)
File "/home/user/venv/project/lib/python3.8/site-packages/django/core/handlers/wsgi.py", line 133, in __call__
response = self.get_response(request)
File "/home/user/venv/project/lib/python3.8/site-packages/django/core/handlers/base.py", line 75, in get_response
response = self._middleware_chain(request)
File "/home/user/venv/project/lib/python3.8/site-packages/django/core/handlers/exception.py", line 36, in inner
response = response_for_exception(request, exc)
File "/home/user/venv/project/lib/python3.8/site-packages/django/core/handlers/exception.py", line 90, in response_for_exception
response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
File "/home/user/venv/project/lib/python3.8/site-packages/django/core/handlers/exception.py", line 129, in handle_uncaught_exception
return callback(request, **param_dict)
File "/home/user/project/src/core/base/views.py", line 236, in error_500_view
response = render(request, "core.base/500.html", context=context)
File "/home/user/venv/project/lib/python3.8/site-packages/django/shortcuts.py", line 19, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "/home/user/venv/project/lib/python3.8/site-packages/django/template/loader.py", line 62, in render_to_string
return template.render(context, request)
File "/home/user/venv/project/lib/python3.8/site-packages/django/template/backends/django.py", line 61, in render
return self.template.render(context)
File "/home/user/venv/project/lib/python3.8/site-packages/django/template/base.py", line 171, in render
return self._render(context)
File "/home/user/venv/project/lib/python3.8/site-packages/django/test/utils.py", line 95, in instrumented_test_render
return self.nodelist.render(context)
File "/home/user/venv/project/lib/python3.8/site-packages/django/template/base.py", line 936, in render
bit = node.render_annotated(context)
File "/home/user/venv/project/lib/python3.8/site-packages/django/template/base.py", line 903, in render_annotated
return self.render(context)
File "/home/user/venv/project/lib/python3.8/site-packages/django/template/loader_tags.py", line 150, in render
return compiled_parent._render(context)
File "/home/user/venv/project/lib/python3.8/site-packages/django/test/utils.py", line 95, in instrumented_test_render
return self.nodelist.render(context)
File "/home/user/venv/project/lib/python3.8/site-packages/django/template/base.py", line 936, in render
bit = node.render_annotated(context)
File "/home/user/venv/project/lib/python3.8/site-packages/django/template/base.py", line 903, in render_annotated
return self.render(context)
File "/home/user/venv/project/lib/python3.8/site-packages/django/templatetags/static.py", line 106, in render
url = self.url(context)
File "/home/user/venv/project/lib/python3.8/site-packages/django/templatetags/static.py", line 103, in url
return self.handle_simple(path)
File "/home/user/venv/project/lib/python3.8/site-packages/django/templatetags/static.py", line 118, in handle_simple
return staticfiles_storage.url(path)
File "/home/user/venv/project/lib/python3.8/site-packages/django/contrib/staticfiles/storage.py", line 153, in url
return self._url(self.stored_name, name, force)
File "/home/user/venv/project/lib/python3.8/site-packages/django/contrib/staticfiles/storage.py", line 132, in _url
hashed_name = hashed_name_func(*args)
File "/home/user/venv/project/lib/python3.8/site-packages/django/contrib/staticfiles/storage.py", line 423, in stored_name
raise ValueError("Missing staticfiles manifest entry for '%s'" % clean_name)
ValueError: Missing staticfiles manifest entry for 'img/pwa/browserconfig.xml'

django context can't find value which exists when used in template tag

In my view render I return the following context dictionary:
company = get_object_or_404(Company, slug=slug)
context = {
'company':company,
}
return render(request, 'company-detail.html', context)
I use this in a template tag to check ownership:
#register.filter(name='is_owner')
def is_owner(user, company):
if user.customer.company.id == company.id:
return True
else:
return False
I try to see the output of the tag in my template:
{{user|is_owner:customer.id}}
I get the following error:
Internal Server Error: /companies/my-company/ Traceback (most recent call last): File "/django/template/base.py", line 835, in
_resolve_lookup
current = current[bit] File "/django/template/context.py", line 83, in __getitem__
raise KeyError(key) KeyError: 'customer'
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "/django/template/base.py", line 841, in _resolve_lookup
if isinstance(current, BaseContext) and getattr(type(current), bit): AttributeError: type object 'RequestContext' has no attribute 'customer'
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "/django/template/base.py", line 849, in _resolve_lookup
current = current[int(bit)] ValueError: invalid literal for int() with base 10: 'customer'
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "/django/core/handlers/exception.py", line 35, in inner
response = get_response(request) File "/django/core/handlers/base.py", line 128, in _get_response
response = self.process_exception_by_middleware(e, request) File "/django/core/handlers/base.py", line 126, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/companies/views.py", line 250, in company_detail_view
return render(request, 'companies/company-detail.html', context) File "/django/shortcuts.py", line 36, in render
content = loader.render_to_string(template_name, context, request, using=using) File "/django/template/loader.py", line 62, in render_to_string
return template.render(context, request) File "/django/template/backends/django.py", line 61, in render
return self.template.render(context) File "/django/template/base.py", line 175, in render
return self._render(context) File "/django/template/base.py", line 167, in _render
return self.nodelist.render(context) File "/django/template/base.py", line 943, in render
bit = node.render_annotated(context) File "/django/template/base.py", line 910, in render_annotated
return self.render(context) File "/django/template/loader_tags.py", line 155, in render
return compiled_parent._render(context) File "/django/template/base.py", line 167, in _render
return self.nodelist.render(context) File "/django/template/base.py", line 943, in render
bit = node.render_annotated(context) File "/django/template/base.py", line 910, in render_annotated
return self.render(context) File "/django/template/loader_tags.py", line 67, in render
result = block.nodelist.render(context) File "/django/template/base.py", line 943, in render
bit = node.render_annotated(context) File "/django/template/base.py", line 910, in render_annotated
return self.render(context) File "/django/template/loader_tags.py", line 194, in render
return template.render(context) File "/django/template/base.py", line 177, in render
return self._render(context) File "/django/template/base.py", line 167, in _render
return self.nodelist.render(context) File "/django/template/base.py", line 943, in render
bit = node.render_annotated(context) File "/django/template/base.py", line 910, in render_annotated
return self.render(context) File "/django/template/base.py", line 993, in render
output = self.filter_expression.resolve(context) File "/django/template/base.py", line 697, in resolve
arg_vals.append(arg.resolve(context)) File "/django/template/base.py", line 802, in resolve
value = self._resolve_lookup(context) File "/django/template/base.py", line 856, in _resolve_lookup
(bit, current)) # missing attribute django.template.base.VariableDoesNotExist: Failed lookup for key [customer] in [{'True': True, 'False': False, 'None': None}, {}, {}, {'company': <Company: My Company>}] [08/May/2018 18:55:25] "GET /companies/my-company/ HTTP/1.1" 500 214439
Your view doesn’t set customer in the template context. Perhaps you want:
{{ user|is_owner:company }}
Note that I’ve passed company instead of company_id because the tag uses company.id.

Password reset showing 500 error in Open edX

I've problem in my Open edX instance when tried to reset user password. it show 500 error server.
the log is
Nov 9 02:16:59 ip-172-31-6-86 [service_variant=lms][openedx.core.djangoapps.user_api.helpers][env:sandbox] ERROR [ip-172-31-6-86 2138] [helpers.py:75] - An unexpected error occurred when calling 'request_password_change' with arguments '(u'ariestiyansyah.rizky#gmail.com', 'xxx.org', True)' and keyword arguments '{}': ValueError('incomplete format',)
Traceback (most recent call last):
File "/edx/app/edxapp/edx-platform/openedx/core/djangoapps/user_api/helpers.py", line 46, in _wrapped
return func(*args, **kwargs)
File "/edx/app/edxapp/edx-platform/openedx/core/djangoapps/user_api/accounts/api.py", line 403, in request_password_change
use_https=is_secure
File "/edx/app/edxapp/edx-platform/common/djangoapps/student/forms.py", line 78, in save
email = loader.render_to_string(email_template_name, context)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/template/loader.py", line 171, in render_to_string
return t.render(Context(dictionary))
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/template/base.py", line 140, in render
return self._render(context)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/template/base.py", line 134, in _render
return self.nodelist.render(context)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/template/base.py", line 823, in render
bit = self.render_node(node, context)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/template/base.py", line 837, in render_node
return node.render(context)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/template/defaulttags.py", line 31, in render
output = self.nodelist.render(context)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/template/base.py", line 823, in render
bit = self.render_node(node, context)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/template/base.py", line 837, in render_node
return node.render(context)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/templatetags/i18n.py", line 151, in render
result = result % data
ValueError: incomplete format
Nov 9 02:16:59 ip-172-31-6-86 [service_variant=lms][root][env:sandbox] ERROR [ip-172-31-6-86 2138] [exceptions.py:9] - Uncaught exception from <class 'django.core.handlers.wsgi.WSGIHandler'>
Traceback (most recent call last):
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 109, in get_response
response = callback(request, *callback_args, **callback_kwargs)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/views/decorators/http.py", line 41, in inner
return func(request, *args, **kwargs)
File "/edx/app/edxapp/edx-platform/lms/djangoapps/student_account/views.py", line 153, in password_change_request_handler
request_password_change(email, request.get_host(), request.is_secure())
File "/edx/app/edxapp/edx-platform/openedx/core/djangoapps/user_api/helpers.py", line 76, in _wrapped
raise api_error(msg)
UserAPIInternalError: An unexpected error occurred when calling 'request_password_change' with arguments '(u'ariestiyansyah.rizky#gmail.com', 'xxx.org', True)' and keyword arguments '{}': ValueError('incomplete format',)
Nov 9 02:16:59 ip-172-31-6-86 [service_variant=lms][django.request][env:sandbox] ERROR [ip-172-31-6-86 2138] [base.py:213] - Internal Server Error: /account/password
Traceback (most recent call last):
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 109, in get_response
response = callback(request, *callback_args, **callback_kwargs)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/views/decorators/http.py", line 41, in inner
return func(request, *args, **kwargs)
File "/edx/app/edxapp/edx-platform/lms/djangoapps/student_account/views.py", line 153, in password_change_request_handler
request_password_change(email, request.get_host(), request.is_secure())
File "/edx/app/edxapp/edx-platform/openedx/core/djangoapps/user_api/helpers.py", line 76, in _wrapped
raise api_error(msg)
UserAPIInternalError: An unexpected error occurred when calling 'request_password_change' with arguments '(u'ariestiyansyah.rizky#gmail.com', 'xxx.org', True)' and keyword arguments '{}': ValueError('incomplete format',)
Can anyone help me solve this problem? thank you so much
This error caused by missing translation, explore and fix the translation.

Django Search Query Fails

I've implemented this search feature on a small site I'm working on and I am having problems when I carry out a search query. This is the traceback:
Traceback (most recent call last):
File "/Users/glosseyvilly/Desktop/web_dev/ccdev/lib/python2.6/site-packages/django/contrib/staticfiles/handlers.py", line 68, in __call__
return self.application(environ, start_response)
File "/Users/glosseyvilly/Desktop/web_dev/ccdev/lib/python2.6/site-packages/django/contrib/staticfiles/handlers.py", line 68, in __call__
return self.application(environ, start_response)
File "/Users/glosseyvilly/Desktop/web_dev/ccdev/lib/python2.6/site-packages/django/core/handlers/wsgi.py", line 272, in __call__
response = self.get_response(request)
File "/Users/glosseyvilly/Desktop/web_dev/ccdev/lib/python2.6/site-packages/django/core/handlers/base.py", line 169, in get_response
response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
File "/Users/glosseyvilly/Desktop/web_dev/ccdev/lib/python2.6/site-packages/django/core/handlers/base.py", line 203, in handle_uncaught_exception
return debug.technical_500_response(request, *exc_info)
File "/Users/glosseyvilly/Desktop/web_dev/ccdev/lib/python2.6/site-packages/django/core/handlers/base.py", line 111, in get_response
response = callback(request, *callback_args, **callback_kwargs)
File "/Users/glosseyvilly/PycharmProjects/first_django_dev/apps/photos/views.py", line 113, in search_photos
'search_query': search_query
File "/Users/glosseyvilly/Desktop/web_dev/ccdev/lib/python2.6/site-packages/django/shortcuts/__init__.py", line 44, in render
return HttpResponse(loader.render_to_string(*args, **kwargs),
File "/Users/glosseyvilly/Desktop/web_dev/ccdev/lib/python2.6/site-packages/django/template/loader.py", line 188, in render_to_string
return t.render(context_instance)
File "/Users/glosseyvilly/Desktop/web_dev/ccdev/lib/python2.6/site-packages/django/template/base.py", line 123, in render
return self._render(context)
File "/Users/glosseyvilly/Desktop/web_dev/ccdev/lib/python2.6/site-packages/django/template/base.py", line 117, in _render
return self.nodelist.render(context)
File "/Users/glosseyvilly/Desktop/web_dev/ccdev/lib/python2.6/site-packages/django/template/base.py", line 744, in render
bits.append(self.render_node(node, context))
File "/Users/glosseyvilly/Desktop/web_dev/ccdev/lib/python2.6/site-packages/django/template/debug.py", line 73, in render_node
result = node.render(context)
File "/Users/glosseyvilly/Desktop/web_dev/ccdev/lib/python2.6/site-packages/django/template/loader_tags.py", line 127, in render
return compiled_parent._render(context)
File "/Users/glosseyvilly/Desktop/web_dev/ccdev/lib/python2.6/site-packages/django/template/base.py", line 117, in _render
return self.nodelist.render(context)
File "/Users/glosseyvilly/Desktop/web_dev/ccdev/lib/python2.6/site-packages/django/template/base.py", line 744, in render
bits.append(self.render_node(node, context))
File "/Users/glosseyvilly/Desktop/web_dev/ccdev/lib/python2.6/site-packages/django/template/debug.py", line 73, in render_node
result = node.render(context)
File "/Users/glosseyvilly/Desktop/web_dev/ccdev/lib/python2.6/site-packages/django/template/loader_tags.py", line 64, in render
result = block.nodelist.render(context)
File "/Users/glosseyvilly/Desktop/web_dev/ccdev/lib/python2.6/site-packages/django/template/base.py", line 744, in render
bits.append(self.render_node(node, context))
File "/Users/glosseyvilly/Desktop/web_dev/ccdev/lib/python2.6/site-packages/django/template/debug.py", line 73, in render_node
result = node.render(context)
File "/Users/glosseyvilly/Desktop/web_dev/ccdev/lib/python2.6/site-packages/django/template/defaulttags.py", line 311, in render
return self.nodelist_true.render(context)
File "/Users/glosseyvilly/Desktop/web_dev/ccdev/lib/python2.6/site-packages/django/template/base.py", line 744, in render
bits.append(self.render_node(node, context))
File "/Users/glosseyvilly/Desktop/web_dev/ccdev/lib/python2.6/site-packages/django/template/debug.py", line 73, in render_node
result = node.render(context)
File "/Users/glosseyvilly/Desktop/web_dev/ccdev/lib/python2.6/site-packages/django/template/defaulttags.py", line 227, in render
nodelist.append(node.render(context))
File "/Users/glosseyvilly/Desktop/web_dev/ccdev/lib/python2.6/site-packages/django/template/base.py", line 915, in render
resolved_vars = [var.resolve(context) for var in self.vars_to_resolve]
File "/Users/glosseyvilly/Desktop/web_dev/ccdev/lib/python2.6/site-packages/django/template/base.py", line 653, in resolve
value = self._resolve_lookup(context)
File "/Users/glosseyvilly/Desktop/web_dev/ccdev/lib/python2.6/site-packages/django/template/base.py", line 692, in _resolve_lookup
raise VariableDoesNotExist("Failed lookup for key [%s] in %r", (bit, current)) # missing attribute
TemplateSyntaxError: Caught VariableDoesNotExist while rendering: Failed lookup for key [user] in u"'uploaded_photos/Published/2011/December/21/image_5.jpeg'"
This is my view function:
def search_photos(request):
"""
"""
search_query = ''
results = None
if ('q' in request.GET) and request.GET['q'].strip():
search_query = request.GET['q']
photo_query = get_query(search_query, ['caption',])
results = Photo.objects.filter(photo_query).order_by('-date_added')
return render(request, 'photos/search.html', {
'results': results,
'search_query': search_query
})
NB: The traceback highlights 'search_query': search_query being a possible error and I'm not sure why.
EDIT: Here is a link to my search.html template : gist.github.com/1524911
You need to format the error message as a code block.
The specific error is at the end of the traceback: Failed lookup for key [user] in u"'uploaded_photos/Published/2011/December/21/image_5.jpeg'" which occurred in _resolve_lookup. This is the routine that does the variable value fetching for templates.
I would take a hard look at the template. Pay special attention to any tricky loop references or template tags that might be doing strange lookups (e.g {% expr %} comes to mind). Let us know what you come up with.
Update:
My money is on the line:
{% phileo_widget result.user result %}
Does the result object have an attribute named user?
Shouldn't your method take self as its first parameter?:
def search_photos(self):
So, you access the request object with:
request = self.request

Django Exception NoReverseMatch for an app not installed (anymore)

I was develping a site and once installed django-bb, but at some point i've decided not to use anymore, so i "turned-off" django-bb commenting all reference code. So far all good. On my dev machine everting runs ok. But on the server machine, something is calling for djangobb. I tried to delete all files relatives to djangobb, and it's no way i can get rid of this exception error. And I've checked all my html code and cant find anyware this call for djangobb.
Please anybody can help?
File "/root/Envs/PolyAssocEnv/lib/python2.6/site-packages/django/core/servers/basehttp.py", line 280, in run
self.result = application(self.environ, self.start_response)
File "/root/Envs/PolyAssocEnv/lib/python2.6/site-packages/django/core/servers/basehttp.py", line 674, in __call__
return self.application(environ, start_response)
File "/root/Envs/PolyAssocEnv/lib/python2.6/site-packages/django/core/handlers/wsgi.py", line 248, in __call__
response = self.get_response(request)
File "/root/Envs/PolyAssocEnv/lib/python2.6/site-packages/django/core/handlers/base.py", line 130, in get_response
return self.handle_uncaught_exception(request, resolver, sys.exc_info())
File "/root/Envs/PolyAssocEnv/lib/python2.6/site-packages/django/core/handlers/base.py", line 180, in handle_uncaught_exception
return callback(request, **param_dict)
File "/root/Envs/PolyAssocEnv/lib/python2.6/site-packages/django/utils/decorators.py", line 76, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/root/Envs/PolyAssocEnv/lib/python2.6/site-packages/django/views/defaults.py", line 31, in server_error
return http.HttpResponseServerError(t.render(Context({})))
File "/root/Envs/PolyAssocEnv/lib/python2.6/site-packages/django/template/__init__.py", line 173, in render
return self._render(context)
File "/root/Envs/PolyAssocEnv/lib/python2.6/site-packages/django/template/__init__.py", line 167, in _render
return self.nodelist.render(context)
File "/root/Envs/PolyAssocEnv/lib/python2.6/site-packages/django/template/__init__.py", line 796, in render
bits.append(self.render_node(node, context))
File "/root/Envs/PolyAssocEnv/lib/python2.6/site-packages/django/template/__init__.py", line 809, in render_node
return node.render(context)
File "/root/Envs/PolyAssocEnv/lib/python2.6/site-packages/django/template/defaulttags.py", line 385, in render
raise e
NoReverseMatch: u'djangobb' is not a registered namespace