0
I’m using a function in mine Templatetagsdjango that basically checks if the user is in the group that has the permission for the given button.
The return 'True or False' works well, IE, it shows the button if the user is in the group, but when trying to open the button, I have the following error in the console AttributeError: 'NoneType' object has no attribute 'groups'
. I don’t know if it has anything to do with being a modal that would have to be opened after clicking the button, because another button that has a function of just sending an email and does not change the screen user, works without errors. I did not find on the internet anything like my problem, I appreciate any light.
My Function
@register.filter
def group_permission(user, grupo):
if user.groups.filter(name=grupo):
return True
return False
My Template
<nav class="navbar navbar-expand-sm bg-dark navbar-dark sticky-top">
{% if user|group_permission:'GERENCIA' %}
<a class="btn btn-success dt-create" data-url="{% url 'inventario_create' %}">Adicionar</a>
{% endif %}
</nav>
My View
@login_required
def inventario_create(request):
if request.method == 'POST':
print('entrou no POST do create')
form = InventarioForm(request.POST)
else:
form = InventarioForm()
return save_insert(request, form, 'inventario_create.html')
This is my JS calling the Modal
var ShowForm = function(){
var btn = $(this);
$.ajax({
url: btn.attr("data-url"),
type: 'get',
dataType:'json',
beforeSend: function(data){
waitingDialog.show('Carregando Inventários', {dialogSize: 'sm', progressType: 'success'})
},
success: function(data){
waitingDialog.hide();
$('#myModal .modal-content').html(data.html_form);
$('#myModal').modal('show')
},
error: function (error) {
console.log(error);
alert('error handing here');
}
});
};
**Tracerback do Erro**
Traceback (most recent call last):
File "/Users/rodolfosousa/Documents/GitHub/InventoryPortal/venv/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/Users/rodolfosousa/Documents/GitHub/InventoryPortal/venv/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/rodolfosousa/Documents/GitHub/InventoryPortal/venv/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/rodolfosousa/Documents/GitHub/InventoryPortal/venv/lib/python3.7/site-packages/django/contrib/auth/decorators.py", line 21, in _wrapped_view
return view_func(request, *args, **kwargs)
File "/Users/rodolfosousa/Documents/GitHub/InventoryPortal/inventarios/views.py", line 103, in inventario_create
return save_insert(request, form, 'inventario_create.html')
File "/Users/rodolfosousa/Documents/GitHub/InventoryPortal/venv/lib/python3.7/site-packages/django/contrib/auth/decorators.py", line 21, in _wrapped_view
return view_func(request, *args, **kwargs)
File "/Users/rodolfosousa/Documents/GitHub/InventoryPortal/inventarios/views.py", line 123, in save_insert
data['inventario_list'] = render_to_string('inventario_list.html')
File "/Users/rodolfosousa/Documents/GitHub/InventoryPortal/venv/lib/python3.7/site-packages/django/template/loader.py", line 62, in render_to_string
return template.render(context, request)
File "/Users/rodolfosousa/Documents/GitHub/InventoryPortal/venv/lib/python3.7/site-packages/django/template/backends/django.py", line 61, in render
return self.template.render(context)
File "/Users/rodolfosousa/Documents/GitHub/InventoryPortal/venv/lib/python3.7/site-packages/django/template/base.py", line 171, in render
return self._render(context)
File "/Users/rodolfosousa/Documents/GitHub/InventoryPortal/venv/lib/python3.7/site-packages/django/template/base.py", line 163, in _render
return self.nodelist.render(context)
File "/Users/rodolfosousa/Documents/GitHub/InventoryPortal/venv/lib/python3.7/site-packages/django/template/base.py", line 937, in render
bit = node.render_annotated(context)
File "/Users/rodolfosousa/Documents/GitHub/InventoryPortal/venv/lib/python3.7/site-packages/django/template/base.py", line 904, in render_annotated
return self.render(context)
File "/Users/rodolfosousa/Documents/GitHub/InventoryPortal/venv/lib/python3.7/site-packages/django/template/loader_tags.py", line 150, in render
return compiled_parent._render(context)
File "/Users/rodolfosousa/Documents/GitHub/InventoryPortal/venv/lib/python3.7/site-packages/django/template/base.py", line 163, in _render
return self.nodelist.render(context)
File "/Users/rodolfosousa/Documents/GitHub/InventoryPortal/venv/lib/python3.7/site-packages/django/template/base.py", line 937, in render
bit = node.render_annotated(context)
File "/Users/rodolfosousa/Documents/GitHub/InventoryPortal/venv/lib/python3.7/site-packages/django/template/base.py", line 904, in render_annotated
return self.render(context)
File "/Users/rodolfosousa/Documents/GitHub/InventoryPortal/venv/lib/python3.7/site-packages/django/template/loader_tags.py", line 62, in render
result = block.nodelist.render(context)
File "/Users/rodolfosousa/Documents/GitHub/InventoryPortal/venv/lib/python3.7/site-packages/django/template/base.py", line 937, in render
bit = node.render_annotated(context)
File "/Users/rodolfosousa/Documents/GitHub/InventoryPortal/venv/lib/python3.7/site-packages/django/template/base.py", line 904, in render_annotated
return self.render(context)
File "/Users/rodolfosousa/Documents/GitHub/InventoryPortal/venv/lib/python3.7/site-packages/django/template/defaulttags.py", line 302, in render
match = condition.eval(context)
File "/Users/rodolfosousa/Documents/GitHub/InventoryPortal/venv/lib/python3.7/site-packages/django/template/defaulttags.py", line 876, in eval
return self.value.resolve(context, ignore_failures=True)
File "/Users/rodolfosousa/Documents/GitHub/InventoryPortal/venv/lib/python3.7/site-packages/django/template/base.py", line 698, in resolve
new_obj = func(obj, *arg_vals)
File "/Users/rodolfosousa/Documents/GitHub/InventoryPortal/inventarios/templatetags/check_permission.py", line 15, in group_permission
if user.groups.filter(name=grupo):
AttributeError: 'NoneType' object has no attribute 'groups'
At first glance it seems that your user is None, in the group_permission function. The information you posted doesn’t seem to be enough for anyone to know exactly what’s going on
– Miguel
@Miguel, is exactly that, doubt and know pq because in the modal q opens, the user is loaded, I succeed if I load it this way in the template {{ form.user }}.
– Rodolfo Sousa