django-authentication

Get user information in django templates

懵懂的女人 提交于 2019-11-27 01:28:29
问题 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 回答1: An alternate method for current Django versions: {% if user.is_authenticated %} <p>Welcome, {{ user.get_username }}. Thanks for logging in.</p> {% else %} <p>Welcome, new user. Please log in.</p> {% endif %} Note: Use request.user.get_username() in views

Suppress “?next=blah” behavior in django's login_required decorator

三世轮回 提交于 2019-11-27 01:20:48
问题 I love django's @login_required decorator, but there's one thing I can't figure out how to make it do. If an unauthenticated user tries visits a @login_required page (e.g. "/private-stuff/"), I want to kick them back to the home page (e.g. "/home/"). But I don't want to append a "?next=" argument to the url. In other words, I just want to redirect to "/home/", not "/home/?next=/private-stuff/". How can I do that? Is there a better way than just writing my own decorator? 回答1: Well, there's two

Django create custom UserCreationForm

六月ゝ 毕业季﹏ 提交于 2019-11-27 00:37:58
问题 I enabled the user auth module in Django, but when I use UserCreationForm he ask me only username and the two password/password confirmation fields. I want also email and fullname fields, and set to required fields. I've done this: from django.contrib.auth.forms import UserCreationForm from django import forms from django.contrib.auth.models import User class RegisterForm(UserCreationForm): email = forms.EmailField(label = "Email") fullname = forms.CharField(label = "Full name") class Meta:

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

断了今生、忘了曾经 提交于 2019-11-26 22:20:20
问题 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", "is_active": 1, "password": "sha1$72cd3$4935449e2cd7efb8b3723fb9958fe3bb100a30f2", ... } } I've left out the seemingly unimportant parts. 2) Use 'create_user' in the setUp function (although I'd rather keep everything in my fixtures class): def setUp(self): User.objects.create_user('homer', 'ho...@simpson.net',

Django: Why create a OneToOne to UserProfile instead of subclassing auth.User?

。_饼干妹妹 提交于 2019-11-26 20:28:13
问题 Note: If you are tempted to 'answer' this question by telling me that you don't like django.contrib.auth, please move on. That will not be helpful. I am well aware of the range and strength of opinions on this matter. Now, the question: The convention is to create a model, UserProfile, with a OneToOne to User. In every way I can think of, a more efficient and effective approach is to subclass User to a class that one intends to use for every human in the system - a class called, say, Person

Putting a django login form on every page

怎甘沉沦 提交于 2019-11-26 19:34:35
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 there an accepted way of doing this? asciitaxi Ok, I eventually found a way of doing this, although I'm sure

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

泪湿孤枕 提交于 2019-11-26 19:27:12
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.) 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 amazing!) 2) Add this middleware: from django.contrib.sessions.models import Session from tracking.models

Check permission inside a template in Django

浪子不回头ぞ 提交于 2019-11-26 18:11:49
问题 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"? 回答1: 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

Change Django ModelChoiceField to show users' full names rather than usernames

我的梦境 提交于 2019-11-26 17:36:16
问题 I have a form in my Django app (not in admin) that allows staff members to select a user from a dropdown. forms.ModelChoiceField(queryset = User.objects.filter(is_staff=False), required = False) The problem is that the dropdown shows users by usernames whereas I'd rather it show their full name from user.get_full_name() and use username only if that is not available. I only really need this change on this page, in other places like admin, I don't care if it uses username. Is there a way I can

Django: Populate user ID when saving a model

浪子不回头ぞ 提交于 2019-11-26 15:06:48
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? Daniel Roseman 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 an extra parameter - request - and store it as an attribute of the form, then also override