How can I detect multiple logins into a Django web application from different locations?

Not sure if this is still needed but thought I would share my solution: 1) Install django-tracking (thankyou for that tip Van Gale Google Maps + GeoIP is amazing!) 2) Add this middleware: from django.contrib.sessions.models import Session from tracking.models import Visitor from datetime import datetime class UserRestrictMiddleware(object): “”” Prevents more than one user logging in … Read more

NoReverseMatch Error

You don’t show where you are trying to reverse this URL, but it looks like you have double-quoted it. If you’re using the url tag, note that you don’t need quotes around the url name: {% url django.contrib.auth.views.login %} not {% url ‘django.contrib.auth.views.login’ %}

How to get the currently logged in user’s id in Django?

First make sure you have SessionMiddleware and AuthenticationMiddleware middlewares added to your MIDDLEWARE_CLASSES setting. The current user is in request object, you can get it by: def sample_view(request): current_user = request.user print current_user.id request.user will give you a User object representing the currently logged-in user. If a user isn’t currently logged in, request.user will be … Read more

What’s the best way to extend the User model in Django?

The least painful and indeed Django-recommended way of doing this is through a OneToOneField(User) property. Extending the existing User model … If you wish to store information related to User, you can use a one-to-one relationship to a model containing the fields for additional information. This one-to-one model is often called a profile model, as … Read more

How to use TokenAuthentication for API in django-rest-framework

“how can I send the token with post request to my api” From the docs… For clients to authenticate, the token key should be included in the Authorization HTTP header. The key should be prefixed by the string literal “Token”, with whitespace separating the two strings. For example: Authorization: Token 9944b09199c62bcf9418ad846dd0e4bbdfc6ee4b “at api side how … Read more

AttributeError: ‘Manager’ object has no attribute ‘get_by_natural_key’ error in Django?

You have created a new user model but you have not yet specified a manager for that model. If you’re not yet familiar with managers in Django I suggest reading the documentation on that first. As the Django 1.5 say (source): You should also define a custom manager for your User model. If your User … Read more

How to get user permissions?

to get all the permissions of a given user, also the permissions associated with a group this user is part of: from django.contrib.auth.models import Permission def get_user_permissions(user): if user.is_superuser: return Permission.objects.all() return user.user_permissions.all() | Permission.objects.filter(group__user=user)