Maybe we can do: 

```python
if hasattr(request, 'user'):
    context['user_id'] = ''
    try:
       context['user_id'] = request.user.pk
    except AttributeError:
       log.error('User has not pk...')       
else:
    context['user_id'] = ''
```