django-authentication

Check permission inside a template in Django

不想你离开。 提交于 2019-11-27 13:23:50
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"? Victor Neo If you are looking to check for permissions in templates, the following code would suffice: {% if perms.app_label.can_do_something %} <form here> {% endif %} Where model refers to the model that the user need permissions to see the form for. Refer to https://docs.djangoproject.com/en/stable/topics/auth/default/#permissions for more examples. The

How can make Django permission_required decorator not to redirect already logged-in users to login page, but display some message

空扰寡人 提交于 2019-11-27 12:40:03
问题 How can make Django permission_required decorator not to redirect already logged-in users to login page, but display some message like Insufficient permissions? Thank you. 回答1: A quick and dirty solution would be to write your own decorator to do this. Something like this: decorator_with_arguments = lambda decorator: lambda *args, **kwargs: lambda func: decorator(func, *args, **kwargs) @decorator_with_arguments def custom_permission_required(function, perm): def _function(request, *args, *

Best way to add convenience methods to a Django Auth User model?

泪湿孤枕 提交于 2019-11-27 11:47:43
问题 I want to add a convenience/model method to the django.contrib.auth.models.User model. What is the best practice for doing this since, last time I checked, extending the User model was considered bad practice. I have a separate custom UserProfile model. Should I be using that for all User-related convenience methods? 回答1: It depends what you are trying to add to the model. If you want to add more information about the user, then it is generally recommended that you use the UserProfile method:

How to force user logout in django?

。_饼干妹妹 提交于 2019-11-27 10:13:28
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? I don't think there is a sanctioned way to do this in Django yet. The user id is stored in the session object, but

Using Django auth UserAdmin for a custom user model

瘦欲@ 提交于 2019-11-27 10:09:33
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, blank=True) This shows up in admin almost like Django's standard User . However, the most important

Log in user using either email address or username in Django

五迷三道 提交于 2019-11-27 09:46:49
问题 I am trying to create an auth backend to allow my users to log in using either their email address or their username in Django 1.6 with a custom user model. The backend works when I log in with a user name but for some reason does not with an email. Is there something I am forgetting to do? from django.conf import settings from django.contrib.auth.models import User class EmailOrUsernameModelBackend(object): """ This is a ModelBacked that allows authentication with either a username or an

Manually logging in a user without password

六眼飞鱼酱① 提交于 2019-11-27 09:14:09
问题 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 now exists but is marked not enabled) User opens email, clicks link (Account is enabled) Thank you! You can now use the site What I'm trying to do is log in the user after he has clicked the email link so he can start using the website right away. I can

How to deploy an HTTPS-only site, with Django/nginx?

半腔热情 提交于 2019-11-27 06:12:45
My original question was how to enable HTTPS for a Django login page , and the only response, recommended that I - make the entire site as HTTPS-only . Given that I'm using Django 1.3 and nginx, what's the correct way to make a site HTTPS-only? The one response mentioned a middleware solution , but had the caveat: Django can't perform a SSL redirect while maintaining POST data. Please structure your views so that redirects only occur during GETs. A question on Server Fault about nginx rewriting to https , also mentioned problems with POSTs losing data, and I'm not familiar enough with nginx to

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

﹥>﹥吖頭↗ 提交于 2019-11-27 05:58:00
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? miku 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 [<Group: Editor>] . Alternatively, and more directly, you can check if a a user is in a group by: if

NoReverseMatch Error

强颜欢笑 提交于 2019-11-27 02:30:27
问题 I keep getting this error for the django login system. Here is part of my urls.py: (r'^contractManagement/login', 'django.contrib.auth.views.login', {'template_name': 'login.html'}), The exact error I am getting: Exception Type: NoReverseMatch Exception Value: Reverse for ''django.contrib.auth.views.login'' with arguments '()' and keyword arguments '{}' not found. I can't understand why i am getting this error. If you need anything else let me know. 回答1: You don't show where you are trying to