I am using the latest version of django and python 3, When I log in I
get the below error message.
django login() takes 1 positional argument but 2 were given
Please find the code for my login view below.
from django.shortcuts import render, get_object_or_404,redirect
from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login
from authentication.forms import LoginForm, ForgottenPasswordForm, ResetPasswordForm
from authentication.functions import send_user_reset_password_link, resend_password_reset_link
from authentication.models import ResetPassword
def login(request):
error_message = None
heading = 'Login Form'
if request.method == 'POST':
form = LoginForm(request.POST)
if form.is_valid():
username = form.cleaned_data['username']
password = form.cleaned_data['password']
remember_me = form.cleaned_data['remember_me']
user = authenticate(request,username=username, password=password)
if not request.POST.get('remember_me', None):
if user is not None:
login(request, user)
return redirect('property_index',user.id)
else:
error_message = 'No login credentials found'
form = LoginForm()
return render(request,'authentication/forms/login.html',{
'form':form,
'error_message':error_message,
'heading':heading
The trouble is: you override the original django login function. So
you should change import.
from django.contrib.auth import authenticate, login as dj_login
and use
dj_login(request, user)
会话保存的login(request, user, backend=None)和文件内登录函数login(request)重名了。
Django自定义的模型如何使用内置的login方法? ٩( ‘ω‘ )و Django问题
django login() takes 1 positional argument but 2 were given
目录问题:Answer 1总结:问题:I am using the latest version of django and python 3, When I log in Iget the below error message.django login() takes 1 positional argument but 2 were givenPlease find the code for my login view below.from django.shortcuts import
Django提供了多种装饰器, 其中login_required可能是经常会使用到的。 这里介绍下四种使用此装饰器的办法。
当然, 在使用前, 记得在工程目录的settings.py中设置好LOGIN_URL
1. URLconf中装饰
from django.contrib.auth.decorators import login_required, permission_required
from django.views.generic import TemplateView
from .views import VoteView
urlpatterns = [
TypeError: test
login()
takes 1
positional argument but 2 were
given
错误意思是 test
login()方法只需要一个参数,但是实际给了两个参数。
下面的代码块是调用
login.py 文件下
Login类 下的 test_
login( ) 方法
def test_case(self):
#执行的测试用例
报错" init() takes 1 positional argument but 2 were given "
解决:在App的urls.py文件中添加as_view()方法
urlpatterns = [
path('', views.ArticleList.as_view(), name='list'),
如果继续报错:" AttributeError: ‘function’ object has no attribute ‘as_view’ "
可能是装饰器出问题了,比如在类视图上使
使用python3的时候,刚开始使用,把所有的都写在了一个文件中,这不符合类的六大设计原则。就把原来的代码按照类进行了拆分。
关于类的六大设计原则:开放关闭原则,里氏替换原则,依赖倒转原则,接口隔离原则,迪米特原则。这里不作过多的解释,大家可以自行百度。
拆分完之后,使用main方法进行实例调用的时候,发现一直报错“takes 1 positional argument but 2 we...
之前写程序经常会碰到此类问题,确认发现并没有少参数。函数为类下函数,定义时需要添加self参数。
但是!但是!为何Python给self赋值而你不必给self赋值?
创建了一个类MyClass,实例化MyClass得到了MyObject这个对象,
然后调用这个对象的方法MyObject.method(arg1,arg2),
这个过程中Python会自动转为Myclass.mehod(My...
Python点击Pycharm按钮Run的时候出现Type ‘manage.py help <subcommand>‘ for help on a specific subcommand.
Python项目迁移 - 报错 - cannot set up a python sdk Python项目迁移时虚拟环境无法成功导入 - pyvenv.cfg - 全局python路径问题
2. Windows 10 - Nvm —— node版本管理工具的安装配置方法 - 报错问题集锦与解决方法 - 工程化 Node 绝对环境隔离 - Node 进阶
CSDN-Ada助手:
关于Mysql数据库连接Navicat时出现1251报错问题的解决方法
m0_73588329:
关于Mysql数据库连接Navicat时出现1251报错问题的解决方法
关于Mysql数据库连接Navicat时出现1251报错问题的解决方法
m0_73588329:
LInux rar 命令压缩文件夹(目录)
CSDN-Ada助手: