Django 5: лучший способ создания AJAX-формы регистрации через UserCreationForm

Как бы просто это ни звучало, но какой эффективный способ создания регистрации пользователя:

forms.py:

from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User

class RegisterForm(UserCreationForm):
    email = forms.EmailField(required=True)

    class Meta:
        model = User
        fields = ("username", "email", "password1", "password2")

    def save(self, commit=True):
        user = super(RegisterForm, self).save(commit=False)
        user.email = self.cleaned_data["email"]
        if commit:
            user.save()
        return user

HTML - index.html:

  <div class="container">
    <form method="post" action="{% url 'base_app:register' %}">
      {% csrf_token %}
      {{ form.as_p }}
      <button type="submit" class="btn btn-primary">Register</button>
  </form>
  </div>

HTML - register.html:

{% block content %}
<div class="container">
    <h2>Register</h2>
    <form method="post">
        {% csrf_token %}
        {{ form.as_p }}
        <button type="submit" class="btn btn-primary" action="">Register</button>
    </form>
    <a href="{% url 'base_app:index' %}">Already have an account? Log in here.</a>
</div>
{% endblock %}

views.py:

class RegisterView(TemplateView):
    template_name = 'base_app/register.html'
    form_class = RegisterForm
    success_url = reverse_lazy('/mvp/')  # Update this to your actual success URL

    def form_valid(self, form):
        form.save()
        return super().form_valid(form)

    def get(self, request, *args, **kwargs):
        form = RegisterForm()
        return render(request, self.template_name, {'form': form})

urls.py:

path('register/', views.RegisterView.as_view(), name='register'),

Я получаю следующую ошибку: [29/Feb/2024 23:14:08] "POST /register/ HTTP/1.1" 405 0

Вернуться на верх