属性错误: 'RegisterPatientView'对象没有属性'object'



当我运行项目并尝试为患者或医生创建注册时,会显示此错误。错误下面显示返回HttpResponseRedirect(self.get_success_url(((中的错误

如果我试图访问登录页面,它不会呈现。

下面是我的代码。视图.py

class RegisterPatientView(CreateView):
"""
Provides the ability to register as a Patient.
"""
model = User
form_class = PatientRegistrationForm
template_name = 'accounts/patient/register.html'
success_url = '/'
extra_context = {
'title': 'Register'
}
def dispatch(self, request, *args, **kwargs):
if self.request.user.is_authenticated:
return HttpResponseRedirect(self.get_success_url())
return super().dispatch(self.request, *args, **kwargs)
def post(self, request, *args, **kwargs):
form = self.form_class(data=request.POST)
if form.is_valid():
user = form.save(commit=False)
password = form.cleaned_data.get("password1")
user.set_password(password)
user.save()
return redirect('accounts:login')
else:
return render(request, 'accounts/patient/register.html', {'form': form})

urls.py

from django.urls import path
from .views import *
from appointment.views import *
app_name = "accounts"
urlpatterns = [
path('patient/register', RegisterPatientView.as_view(), name='patient-register'),
path('patient/profile/update/', EditPatientProfileView.as_view(), name='patient-profile-update'),
path('doctor/register', RegisterDoctorView.as_view(), name='doctor-register'),
path('doctor/profile/update/', EditDoctorProfileView.as_view(), name='doctor-profile-update'),
path('login', LoginView.as_view(), name='login'),
path('logout', LogoutView.as_view(), name='logout'),
]

型号.py

from accounts.managers import UserManager
GENDER_CHOICES = (
('male', 'Male'),
('female', 'Female'))

class User(AbstractUser):
username = None
role = models.CharField(max_length=12, error_messages={
'required': "Role must be provided"
})
gender = models.CharField(max_length=10, blank=True, null=True, default="")
email = models.EmailField(unique=True, blank=False,
error_messages={
'unique': "A user with that email already exists.",
})
phone_number = models.CharField(unique=True, blank=True, null=True, max_length=20,
error_messages={
'unique': "A user with that phone number already exists."
})
USERNAME_FIELD = "email"
REQUIRED_FIELDS = []
def __unicode__(self):
return self.email
objects = UserManager()

求你了!帮我一下,如果需要任何其他代码,请告诉我。非常感谢。

错误是因为您在dispatch()中调用self.get_success_url()CreateView.get_success_url()依赖于在CreateView.get()CreateView.post()方法上创建的object字段。

我建议您将您的观点更改为:

class RegisterPatientView(CreateView):
. . .
success_url = '/'
. . .
def dispatch(self, request, *args, **kwargs):
if self.request.user.is_authenticated:
return HttpResponseRedirect(self.success_url)
return super().dispatch(self.request, *args, **kwargs)
def post(self, request, *args, **kwargs):
super().post(request, *args, **kwargs)
form = self.form_class(data=request.POST)
. . .

最新更新