Django展示注册表单时报错:__init__()需1个参数却传入2个
Hey, I see exactly where the issue is here—let's get this sorted out quickly!
The Root Cause
In your URL configuration line:
path('signup/', views.StudentSignUpView, name='signup')
You're passing the class itself (StudentSignUpView) to the path, but Django class-based views require you to call the as_view() method to turn them into a callable view function that can handle requests. When you skip as_view(), Django tries to instantiate the class directly with request arguments, which triggers that "1 positional argument but 2 were given" error.
The Fix
Update your URL path to use as_view():
path('signup/', views.StudentSignUpView.as_view(), name='signup')
Quick Double-Check for Future Stability
While we're at it, let's make sure a couple other critical pieces are in place:
- Ensure your
Studentmodel has a proper one-to-one relationship with your customUsermodel (since you're creating aStudentlinked to the new user):class Student(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) attended_courses = models.ManyToManyField(Course) - Confirm you've registered your custom
Usermodel insettings.py:AUTH_USER_MODEL = 'your_app_name.User'
That should resolve the immediate error and get your student signup form with course selection up and running smoothly!
内容的提问来源于stack exchange,提问作者user9092892

