django-authentication

How to force user logout in django?

你离开我真会死。 提交于 2019-11-26 15:04:52
问题 In my django app under certain conditions I need to be able to force user logout by a username. Not necessarily the current user who is logged in, but some other user. So the request method in my view doesn't have any session information about the user that I want to logout. I am familiar with django.auth, and with auth.logout method, but it takes request as an argument. Is there a "django-way" to log user out if all I have is the username? Or do I have to roll my own logout sql? 回答1: I don't

Using Django auth UserAdmin for a custom user model

泄露秘密 提交于 2019-11-26 15:02:39
问题 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 add your custom profile fields. This class provides the full implementation of the default User as an abstract model. Said and done. I created a new model like below: class MyUser(AbstractUser): some_extra_data = models.CharField(max_length=100,

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

99封情书 提交于 2019-11-26 15:02:18
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? Is it simply a matter of changing 30 to 75 in the source code? username = models.CharField(_('username'), max_length=30, unique=True, help_text=_("Required. 30 characters or fewer. Letters, numbers and @/./+/-/_ characters")) Or is there other validation on this field that would have to be changed or any other repercussions to doing so? See comment

How to use permission_required decorators on django class-based views

妖精的绣舞 提交于 2019-11-26 14:02:17
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 login_required attribute in the view, but I don't know how to do this on the new views. Is there some section in the django docs explaining this? I didn't found anything. What is wrong in my code? I tried to use the @method_decorator but it replies " TypeError at /spaces/prueba/ _wrapped_view() takes at least 1 argument (0 given) " Here is the code (GPL):

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

早过忘川 提交于 2019-11-26 12:16:37
问题 I want to only allow one authenticated session at a time for an individual login in my Django application. So if a user is logged into the webpage on a given IP address, and those same user credentials are used to login from a different IP address I want to do something (either logout the first user or deny access to the second user.) 回答1: 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

How to use permission_required decorators on django class-based views

て烟熏妆下的殇ゞ 提交于 2019-11-26 12:16:10
问题 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 login_required attribute in the view, but I don\'t know how to do this on the new views. Is there some section in the django docs explaining this? I didn\'t found anything. What is wrong in my code? I tried to use the @method_decorator but it replies \"

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

百般思念 提交于 2019-11-26 11:47:59
问题 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? 回答1: You can access the groups simply through the groups attribute on User . from django.contrib.auth.models import User, Group group = Group(name = "Editor") group.save() # save this new group for this example user = User.objects.get(pk = 1) # assuming, there is one initial user user.groups.add(group) # user is now in the "Editor" group then user.groups.all() returns [

Putting a django login form on every page

前提是你 提交于 2019-11-26 07:01:01
问题 I\'d like the login form (AuthenticationForm from django.contrib.auth) to appear on every page in my site if the user is not logged in. When the user logs in, they will be redirected to the same page. If there is an error, the error will be shown on the same page with the form. I suppose you\'d need a context processor to provide the form to every template. But, then you\'d also need every view to handle the posted form? Does this mean you need to create some middleware? I\'m a bit lost. Is

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

北战南征 提交于 2019-11-26 05:57:24
问题 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? Is it simply a matter of changing 30 to 75 in the source code? username = models.CharField(_(\'username\'), max_length=30, unique=True, help_text=_(\"Required. 30 characters or fewer. Letters, numbers and @/./+/-/_ characters\")) Or is there other

Django: Populate user ID when saving a model

折月煮酒 提交于 2019-11-26 04:09:32
问题 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 will not use the built-in Admin. Can anyone advise on how I should go about this? 回答1: If you want something that will work both in the admin and elsewhere, you should use a custom modelform. The basic idea is to override the __init__ method to take