django-authentication

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

爱⌒轻易说出口 提交于 2019-11-29 18:54:23
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 = Game.objects.create(name="gta", owner=?) K Z 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

How to enforce account based separation in Django

随声附和 提交于 2019-11-29 10:28:32
问题 I have a Django app which has a single-account model. We are converting this to be multi-account, so almost every model will have a ForeignKey(Account) . What is the easiest way to make sure that each Account (each account is in its own subdomain) can only access its own data? We have a middleware that populates the subdomain, and the current account on each request. We can do this the hard way, by adding a filter(...., account = request.account) in all of our views. This is not desirable as,

Adding extra_context in Django logout built-in view

寵の児 提交于 2019-11-29 10:24:20
In django/contrib/auth/views.py there is the definition of the logout view : def logout(request, next_page=None, template_name='registration/logged_out.html', redirect_field_name=REDIRECT_FIELD_NAME, current_app=None, extra_context=None): I would like to add extra_context to get rid of the 'Logged out' title that appear when I log off so I'm trying this in my url confs : (r'^accounts/logout/$', logout(extra_context={'title':'something else'}) ), but then I get this error : logout() takes at least 1 non-keyword argument (0 given) what I'm doing wrong? ps: when I do (r'^accounts/logout/$',

How to access user names and profiles with django-allauth

喜欢而已 提交于 2019-11-29 09:26:38
问题 I'm using Django with django-allauth for social authentication. I have authentication up and running, but can anyone give simple examples of how to: show the name and avatar of a logged-in user add some information to a user's account? For example, on the home page, I've got {% if user.is_authenticated %} <li><a href="{% url account_logout %}?next=/">Logout</a></li> {% endif %} That's showing the Logout link correctly, but how would I add the user's name and avatar? Something like (pseudocode

What is the opposite of @login_required decorator for Django views?

孤街醉人 提交于 2019-11-29 08:56:02
问题 If I want to make sure that a view is listed as having public access, is there a decorator equivalent to @public_access which would be the opposite of @login_required and make it clear that the view should be publicly accessible always? One use case I have in mind is to automatically add "@csrf_exempt" to all public views in addition to making it clear in the code that the view should be publicly accessible. 回答1: Unfortunately, there's currently no built-in support for this in Django, leaving

decide where to go to after connecting with django-allauth

百般思念 提交于 2019-11-29 05:06:35
After connecting an account with a social app using django-allauth the user is redirected to accounts/social/connections . How can I change this behavior? pennersr If the user is adding more social accounts to his existing (local) account, then the most logical default would be indeed to redirect to the social account connections management screen. However, you can easily override the default by passing along a next parameter. Have a look here: https://github.com/pennersr/django-allauth/blob/master/allauth/socialaccount/helpers.py#L125 You'll see that the next parameter is checked, falling

django-allauth: how to properly use email_confirmed signal to set user to active

夙愿已清 提交于 2019-11-29 04:04:13
In another post , I mentioned that I was trying to use allauth's email_confirmed signal to change the is_active field on the confirmed user to true. However, the following code gave me the exception "User matching query does not exist." from allauth.account.signals import email_confirmed from django.dispatch import receiver from django.contrib.auth.models import User @receiver(email_confirmed) def email_confirmed_(request, email_address, **kwargs): user = User.objects.get(email=email_address) user.is_active = True user.save() I tried to re-work this, and still got a similar exception,

Django Authenticate Backend Multiple Databases

∥☆過路亽.° 提交于 2019-11-29 02:38:45
I am rewriting a legacy application that has a database for each customer. Each customer has its own authentication and user set. Thus, I'll need a custom authentication backend because django's auth is set to only use default. I have written middleware that examines the url upon every request and extracts information there to set a database_name on the request. If I had access to the request during processing of my custom authencation backend, I could easily perform database calls as user = User.objects.using(request.db).get(username=username) However, I see no easy way to accomplish this. I

How can I not use Django's admin login view?

江枫思渺然 提交于 2019-11-29 02:02:36
问题 I created my own view for login. However if a user goes directly to /admin it brings them to the admin login page and doesn't use my custom view. How can I make it redirect to the login view used for everything not /admin? 回答1: From http://djangosnippets.org/snippets/2127/—wrap the admin login page with login_required . For example, in urls.py : from django.contrib.auth.decorators import login_required from django.contrib import admin admin.autodiscover() admin.site.login = login_required

NoReverseMatch while rendering: Reverse for ''django.contrib.auth.views.login''

佐手、 提交于 2019-11-28 22:23:27
问题 I'm using Django's authentication, and in the login.html template, the following statement is generating an error: {% url 'django.contrib.auth.views.login' %} TemplateSyntaxError at /login Caught NoReverseMatch while rendering: Reverse for ''django.contrib.auth.views.login'' with arguments '()' and keyword arguments '{}' not found. This url is defined in my urls.py: (r'^login$', 'django.contrib.auth.views.login') I have installed the auth system: INSTALLED_APPS = ( 'django.contrib.auth', ...