TypeError at /login login() missing 1 required positional argument: 'User'
The login
view should not contain a User
parameter, in fact for the given view, you do not need to import the User
model at all. Furthermore in order to login, you should use the login(…)
method [Django-doc]. Since you already have a method named login
, you should import it under a different name:
from django.contrib.auth import login as auth_login
# no User parameter
def login(request):
if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(username=username, password=password)
if user is not None:
auth_login(request, user)
return render(request, 'welcome.html',)
else:
messages.info(request , 'invalid credentials')
return render(request, 'login.html')
else:
return render(request, 'welcome.html')
Note: In case of a successful POST request, you should make a
redirect
[Django-doc]
to implement the Post/Redirect/Get pattern [wiki].
This avoids that you make the same POST request when the user refreshes the
browser.
Getting this error in django "login() missing 1 required positional argument: 'user' "
You’ve got the import wrong. You need to import the login
view:
from django.contrib.auth.views import login
At the moment you have imported the login
function that actually logs the user in.
The login
view you are using is deprecated in Django 1.11. You can switch to the new class-based LoginView
:
from django.contrib.auth.views import LoginView
url(r'^login/$', LoginView.as_view(), name='login'),
Get_queryset() missing 1 required positional argument: 'request' when i'm trying to filter objects by logged in user
You can do this
class FacilityListView(ListAPIView):
permission_classes = [IsAuthenticated]
serializer_class = FacilitySerializer
def get_queryset(self, request):
admin_uid = self.request.user.id
return Facility.objects.filter(admin_uid=admin_uid)
Django custom user error when registering: get_session_auth_hash() missing 1 required positional argument: 'self' (new user can otherwise log in)
You've got a typo in login
. You are using User
which is the class instead of user
:
login(request, User) # --> should be login(request, user)
Related Topics
How to Adjust Padding With Cutoff or Overlapping Labels
Python3: Remove a Substring Between Two Delimiting Char
How to Find the Shortest Word in a List in Python
Typeerror: Image Data Can Not Convert to Float
Django Development Server, How to Stop It When It Run in Background
Is There Any Numpy Group by Function
Python: Create 50 Objects Using a for Loop
Change Specific Value in CSV File Via Python
How to Call a Classes Method from Another Class Without Initialising the First Class
Calculate Monthly Returns from Daily Returns in Pandas(Cumpound)
Python - How to Separate Paragraphs from Text
Conversion of String to Upper Case Without Inbuilt Methods
Pandas Get Frequency of Item Occurrences in a Column as Percentage
Removing Punctuations and Spaces in a String Without Using Regex