2

我认为 Django 注册 1.0 的子类:

class RegistrationView(RegistrationView):
    pass

我这样做是因为我现在想通过 AJAX 发布帖子。我不确定我必须覆盖哪些方法?有什么例子或建议吗?

这是我的view.py:

{% block content %}
<form method="post" action="" id="registration">

  {{ form.as_p }}

  <input type="submit" value="{% trans 'Submit' %}" />
</form>



 <script>


$('#registration').submit(function(e){
     e.preventDefault(); //prevent default form submit

   $.ajax({

       url: '/accounts/register/',
       type: 'POST',
       contentType: "application/json;charset=utf-8",
       dataType: "json",
       data: {
            'csrfmiddlewaretoken': '{% csrf_token %}',
            'id_username': $("#id_username").val(),
            'id_email': $("#id_email").val()
            },
        success: function() {
            alert('Test');

        },
        error: function(errorThrown){
            console.log(errorThrown);
            alert('Error');
            alert(errorThrown);
        }
    });



});

django注册1.0查看

class RegistrationView(_RequestPassingFormView):
    """
    Base class for user registration views.

    """
    disallowed_url = 'registration_disallowed'
    form_class = RegistrationForm
    http_method_names = ['get', 'post', 'head', 'options', 'trace']
    success_url = None
    template_name = 'registration/registration_form.html'

    def dispatch(self, request, *args, **kwargs):
        """
        Check that user signup is allowed before even bothering to
        dispatch or do other processing.

        """
        if not self.registration_allowed(request):
            return redirect(self.disallowed_url)
        return super(RegistrationView, self).dispatch(request, *args, **kwargs)

    def form_valid(self, request, form):
        new_user = self.register(request, **form.cleaned_data)
        success_url = self.get_success_url(request, new_user)

        # success_url may be a simple string, or a tuple providing the
        # full argument set for redirect(). Attempting to unpack it
        # tells us which one it is.
        try:
            to, args, kwargs = success_url
            return redirect(to, *args, **kwargs)
        except ValueError:
            return redirect(success_url)

    def registration_allowed(self, request):
        """
        Override this to enable/disable user registration, either
        globally or on a per-request basis.

        """
        return True

    def register(self, request, **cleaned_data):
        """
        Implement user-registration logic here. Access to both the
        request and the full cleaned_data of the registration form is
        available here.

        """
        raise NotImplementedError
4

1 回答 1

4

“registration 1.0”应用程序并没有真正被创建来支持 ajax,所以你有很多重写工作要做。

只是为了使注册工作你需要这样的东西:

import json
from django.http import HttpResponse
from django.views.generic.edit import CreateView
from django.contrib.auth.models import User

from registration.views import RegistrationView


class AjaxableResponseMixin(object):
    def get_form_kwargs(self):
        kwargs = {'initial': self.get_initial()}
        if self.request.method in ('POST', 'PUT'):
            dict_ = self.request.POST.copy()
            del dict_['csrfmiddlewaretoken']
            kwargs.update({
                'data': dict_,
                'files': self.request.FILES,
            })
        return kwargs

    def render_to_json_response(self, context, **response_kwargs):
        data = json.dumps(context)
        response_kwargs['content_type'] = 'application/json'
        return HttpResponse(data, **response_kwargs)

    def form_invalid(self, form):
        response = super(AjaxableResponseMixin, self).form_invalid(form)

        if self.request.is_ajax():
            return self.render_to_json_response(form.errors, status=400)
        else:
            return response

    def form_valid(self, request, form):
        if self.request.is_ajax():
            new_user = self.register(request, **form.cleaned_data)
            data = {
                'pk': new_user.pk,
            }
            return self.render_to_json_response(data)
        else:
            response = super(AjaxableResponseMixin, self).form_valid(request, form)
            return response


class MyRegistrationView(AjaxableResponseMixin, RegistrationView):
    def register(self, request, **cleaned_data):
        del cleaned_data['password2']
        user = User.objects.create_user(data['username'], data['email'], data['password1'])
        return user

网址.py

(r'^accounts/register/', MyRegistrationView.as_view()),
(r'^accounts/', include('registration.backends.default.urls')),

html

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
</head>
<body>
    <form id="registration" action="." method="post">{% csrf_token %}
        {{ form }}
        <input type="submit" name="submit" value="Submit" />
    </form>
    <script type="text/javascript">
        $('#registration').submit(function(e){
            e.preventDefault();
            $.ajax({
                url: '/accounts/register/',
                type: 'POST',
                dataType: "json",
                beforeSend: function (request) {
                    request.setRequestHeader("X-CSRFToken", $('input[name="csrfmiddlewaretoken"]').val());
                },
                data: $('#registration').serialize(),
                success: function() {
                    console.log('success');
                },
                error: function(errorThrown){
                    console.log(errorThrown);
                }
            });
        });
    </script>
</body>
</html>
于 2013-10-08T19:33:18.867 回答