Почему мое представление регистрации в django не может добавлять новых пользователей?
У меня есть нижеприведенный DRF APIView. Я создаю базовую регистрацию пользователя с OTP
class RegisterView(APIView):
permission_classes = [AllowAny]
def get(self, request, *args, **kwargs):
return render(request, 'registration.html')
def post(self, request, *args, **kwargs):
serializer = RegisterSerializer(data=request.data)
if serializer.is_valid():
user = serializer.save()
# Send verification email with OTP
otp = generate_otp()
user.otp = otp
user.otp_expiry = datetime.now() + timedelta(minutes=5)
user.save()
send_verification_email(user.email, otp)
return Response(
{
'message': 'Account created successfully. Please verify your email.',
"status": status.HTTP_201_CREATED,
}
)
return Response(
{
'message': serializer.errors,
"status": status.HTTP_400_BAD_REQUEST,
}
)
Вышеуказанная функция вызывает две нижеуказанные функции
def generate_otp():
return str(random.randint(100000, 999999))
def send_verification_email(email, otp):
subject = "Verify Your Email Address"
template_name = "email_verification.html"
context = {"otp": otp}
html_content = render_to_string(template_name, context)
text_content = strip_tags(html_content)
msg = EmailMessage(subject, text_content, to=[email])
msg.content_subtype = "html"
msg.send()
register.html
<form id="register-form">
{% csrf_token %}
<div class="form-group">
<label for="email">Email Address:</label>
<input type="email" class="form-control" id="email" name="email" required>
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" class="form-control" id="password" name="password" required>
</div>
<button type="submit">Register</button>
</form>
<script>
document.getElementById('register-form').addEventListener('submit', function(event) {
event.preventDefault();
const formData = new FormData(event.target);
const jsonData = {};
for (const [key, value] of formData.entries()) {
jsonData[key] = value;
}
fetch('{% url "register" %}', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(jsonData)
})
.then(response => response.json())
.then(data => {
if (data.status === 201) {
alert('Account created successfully. Please verify your email.');
} else {
alert('Error creating account: ' + data.message);
}
})
.catch(error => {
alert('Error creating account: ' + error.message);
});
});
</script>
urls.py
urlpatterns = [
path('register', RegisterView.as_view(), name='register'),
]
проект: urls.py
path('auth/', include('user_authentication.urls'))
когда я звоню http://127.0.0.1:8000/auth/register
Я вижу следующие ошибки в моей консоли
Forbidden: /auth/register
[04/Jun/2024 16:38:42] "POST /auth/register HTTP/1.1" 403 45
и на веб-странице я вижу
Error creating account: undefined