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.

1640 questions
484
votes
15 answers

Extending the User model with custom fields 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
  • 16,296
  • 21
  • 61
  • 79
257
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
  • 12,720
  • 14
  • 49
  • 71
168
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…
159
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
  • 217,272
  • 324
  • 727
  • 1,038
142
votes
6 answers

How to get the currently logged in user's user 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,465
  • 3
  • 11
  • 7
105
votes
4 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,328
  • 3
  • 17
  • 31
88
votes
6 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,318
  • 10
  • 32
  • 45
86
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…
81
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
  • 16,537
  • 15
  • 52
  • 70
75
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
  • 16,699
  • 9
  • 52
  • 68
65
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
  • 75,812
  • 80
  • 244
  • 374
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
  • 11,281
  • 16
  • 51
  • 69
56
votes
4 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
  • 2,492
  • 3
  • 28
  • 45
55
votes
13 answers

Django: Populate user ID when saving a model

I have a model with a created_by field that is linked to the standard Django User model. I need to automatically populate this with the ID of the current User when the model is saved. I can't do this at the Admin layer, as most parts of the site…
seanf
55
votes
3 answers

Django-AttributeError 'User' object has no attribute 'backend' (But....it does?)

In order to sign users in after registering them, I manually set the user.backend property. It normally works in my views. In this instance, I'm trying to register the user via AJAX. It is raising an AttributeError. Here is my code: def…
Ben
  • 10,944
  • 9
  • 52
  • 88
1
2 3
99 100