Questions tagged [django-authentication]

django-authentication refers to the built-in auth module for authentication & authorization that can be extended.

django-authentication refers to the built-in auth module for authentication & authorization that can be extended. It handles user accounts, groups, permissions and cookie-based user sessions.

See documentation.

1888 questions
541
votes
17 answers

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

What's the best way to extend the User model (bundled with Django's authentication app) with custom fields? I would also possibly like to use the email as the username (for authentication purposes). I've already seen a few ways to do it, but can't…
Farinha
  • 17,636
  • 21
  • 64
  • 80
273
votes
15 answers

Having Django serve downloadable files

I want users on the site to be able to download files whose paths are obscured so they cannot be directly downloaded. For instance, I'd like the URL to be something like this: http://example.com/download/?f=somefile.txt And on the server, I know…
damon
  • 14,485
  • 14
  • 56
  • 75
192
votes
13 answers

In Django, how do I check if a user is in a certain group?

I created a custom group in Django's admin site. In my code, I want to check if a user is in this group. How do I do that?
TIMEX
  • 259,804
  • 351
  • 777
  • 1,080
185
votes
13 answers

How to use permission_required decorators on django class-based views

I'm having a bit of trouble understanding how the new CBVs work. My question is this, I need to require login in all the views, and in some of them, specific permissions. In function-based views I do that with @permission_required() and the…
181
votes
8 answers

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

How to get the currently logged-in user's id? In models.py: class Game(models.model): name = models.CharField(max_length=255) owner = models.ForeignKey(User, related_name='game_user', verbose_name='Owner') In views.py: gta =…
k44
  • 1,855
  • 3
  • 13
  • 7
130
votes
18 answers

Django - Login with Email

I want django to authenticate users via email, not via usernames. One way can be providing email value as username value, but I dont want that. Reason being, I've a url /profile//, hence I cannot have a url…
Praful Bagai
  • 16,684
  • 50
  • 136
  • 267
124
votes
5 answers

Check permission inside a template in Django

Can I use the Auth application's permission checking inside a template in Django? (I want to display a simple form at the end of the template for privileged users) And more importantly, should I do it at all or is this no the "Django way"?
Daniel
  • 2,728
  • 3
  • 21
  • 33
117
votes
8 answers

How to check whether the user is anonymous or not in Django?

def index(request): the_user = request.user In Django, how do I know if it's a real user or not? I tried: if the_user: but "AnonymousUser" is there even if no one logs in. So, it always returns true and this doesn't work.
TIMEX
  • 259,804
  • 351
  • 777
  • 1,080
103
votes
8 answers

Django's self.client.login(...) does not work in unit tests

I have created users for my unit tests in two ways: 1) Create a fixture for "auth.user" that looks roughly like this: { "pk": 1, "model": "auth.user", "fields": { "username": "homer", …
thebossman
  • 4,598
  • 11
  • 34
  • 45
93
votes
6 answers

Using Django auth UserAdmin for a custom user model

From the Django.Contrib.Auth docs: Extending Django’s default User If you’re entirely happy with Django’s User model and you just want to add some additional profile information, you can simply subclass django.contrib.auth.models.AbstractUser and…
85
votes
10 answers

How to force a user logout in Django?

In my Django app under certain conditions I want to be able to force users to log out by a username. Not necessarily the current user who is logged in, but another user. So, the request method in my view doesn't have any session information about…
Sergey Golovchenko
  • 18,203
  • 15
  • 55
  • 72
80
votes
5 answers

Manually logging in a user without password

I hope you can help me figure the best way to implement a manual (server-side initiated) login without using the password. Let me explain the workflow: User registers Thank you! An email with an activation link has been sent blablabla (Account…
Agos
  • 18,542
  • 11
  • 56
  • 70
72
votes
6 answers

Users in initial data fixture

I'm creating a few users by default in my fixtures/initial_data.json so as to have some testing "subjects." The problem I'm experiencing is password generation. I could set the password in the 'fields', but that won't generate a hashed password: [ …
Naftuli Kay
  • 87,710
  • 93
  • 269
  • 411
70
votes
5 answers

Get user information in django templates

What's the best way to get user information from a django template? For example, if I just want to: If the user is logged in, display "Welcome [username]" Otherwise, display the login button. I'm using django-registration/authentication
River Tam
  • 3,096
  • 4
  • 31
  • 51
65
votes
13 answers

Can django's auth_user.username be varchar(75)? How could that be done?

Is there anything wrong with running alter table on auth_user to make username be varchar(75) so it can fit an email? What does that break if anything? If you were to change auth_user.username to be varchar(75) where would you need to modify django?…
Purrell
  • 12,461
  • 16
  • 58
  • 70
1
2 3
99 100