'Object of type date is not JSON serializable (error in tests.py)
I wrote this view to register with the user's phone and the view itself works fine
But in test def test_signup_phone_post (self)
For response = self.client.post (reverse ('signup_phone'), self.user, follow = True)
This gives the error: TypeError: Object of type date is not JSON serializable
views.py
class SignupPhoneView(View):
def get(self, request):
form = SignupPhoneForm()
return render(request, 'user/signup_phone.html', {'form': form})
def post(self, request):
form = SignupPhoneForm(request.POST)
if User.objects.filter(username=request.POST.get("username") + "@mail.com"): # If username already exists
logger_warn.warning(f'class SignupPhoneView, {request.POST.get("username")}, Registration exists')
return redirect("/user/signup-phone/", messages.error(request, "Registration exists"))
elif form.is_valid():
token = random.randint(100000, 999999)
kave_negar_token_send(form.cleaned_data['recovery_phone'], token)
code = OtpCode.objects.create(phone_number=form.cleaned_data['recovery_phone'], code=token)
print(code)
request.session['signup_info'] = {
'username': form.cleaned_data['username'],
'password': form.cleaned_data['password'],
'first_name': form.cleaned_data['first_name'],
'last_name': form.cleaned_data['last_name'],
'birthday': form.cleaned_data['birthday'],
'gender': form.cleaned_data['gender'],
'country': form.cleaned_data['country'],
'recovery_phone': form.cleaned_data['recovery_phone']
}
messages.success(request, f'{request.POST.get("username")} please enter your verification code')
return redirect("/user/phone-verify/")
else:
logger_error.error(f'class SignupEmailView, {request.POST.get("username")}, Registration failed')
messages.error(request, "Registration failed")
return redirect("/user/signup-phone/")
tests.py
class SignupPhoneTest(TestCase):
def setUp(self):
self.user = {
'username': 'testuser',
'password': '7895146',
'recovery_phone': '09874563215',
'first_name': 'lina',
'last_name': 'wong',
'birthday': '01/10/2000',
'gender': 'f',
'country': 'England'
}
def test_signup_phone_page_url(self):
response = self.client.get("/user/signup-phone/")
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, template_name='user/signup_phone.html')
def test_signup_phone_post(self):
response = self.client.post(reverse('signup_phone'), self.user, follow=True)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.redirect_chain, [(reverse('phone_verify'), 302)])
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
