故障排除5秒延迟提交表格

时间:2016-09-13 18:17:14

标签: python django debugging profiling django-debug-toolbar

我创建了一个非常简单的表单提交供用户注册,要求他们输入他们的电子邮件,用户名和密码。从单击提交按钮到表单实际提交时间延迟约5秒。我怎么能弄清楚这里发生了什么?这是我到目前为止所尝试的:

Django调试工具栏 - 分析:

这里似乎有线索,但我无法使用此信息来解决使用此数据的问题。有任何想法吗? Profiling Image

的Javascript

我使用NProgress附加了一些进度条Javascript。有趣的是,进度条直到之后 5秒延迟才开始。这是HTML(orange-bt类只是格式化):

<form class="register-form" method="POST" action="/register/">
    {% csrf_token %}
    {{ form.as_p }}
    <input class="progress-bar orange-bt" type="submit" value="Register">
</form>

底部有一些Javascript:

<script>
    $(".progress-bar").click(function() { NProgress.start(); });
</script>

views.py

我在排除故障时向控制台线插入了一些打印件,似乎延迟发生在 if request.method ==&#39; POST&#39; 行之前。 POST if语句中的所有动作在5秒延迟后足够快地发生,但是我无法弄清楚这些5秒的执行是什么。它让我疯了!

@render_with('users/register.html')
def register(request, user_id=None):
    """
        Register page that tracks referal
        handle post request for registering new users via normal registration
    """

    if not request.user.is_anonymous():
        return HttpResponseRedirect('/dashboard/')

    #Storing invited_by in user session if they were invited to register
    if user_id:
        user = get_object_or_404(User, pk=user_id)
        request.session['invited_by'] = user.pk
    else:
        user = None

    settings = WeHealthSetting.objects.all().last()

    if request.method == 'POST':
        form = RegisterForm(request.POST)
        if form.is_valid():
            # Handling POST data
            username = form.cleaned_data['username']
            email = form.cleaned_data['email']
            password = form.cleaned_data['password']

            # Assign form data to the User model
            user = User()
            user.username = username
            user.email = email
            user.set_password(password)
            user.registration_type = 1
            user.is_active = True

            #Save the user
            user.save()

            #Subscribe to mailchimp
            subscribe_to_mailchimp('336395d8de', user.email)

            #Login user
            user = authenticate(username=username, password=password)
            if user:
                if user.is_active:
                    login(request, user)

            #Send welcome email
            url = '/register/%d/' % (request.user.pk)
            send_email('initial_email','Invite from WeHealth', request.user.email,
                         ['initial_email'],
                         [  
                            dict(name='url', content=str(url))
                         ]
             )

            #Direct to Dashboard
            return HttpResponseRedirect('/dashboard/')

        else:
            return dict(invited_by=user, form=form, settings=settings)
    else:
        form = RegisterForm()

    return dict(invited_by=user, form=RegisterForm, settings=settings)

forms.py

这是forms.py。验证过程会导致延迟吗?

class RegisterForm(forms.Form):
    username = forms.CharField(required=True,  widget=forms.TextInput(attrs={'class':'form-input', 'placeholder': 'Username (for your profile)', 'autocomplete':'off'}))
    email = forms.EmailField(required=True,  widget=forms.EmailInput(attrs={'class':'form-input', 'placeholder': 'Email (will not show on profile)'}))
    password = forms.CharField(widget=forms.PasswordInput(attrs={'class':'form-input', 'placeholder': 'Password'}), required=True)

    def clean_username(self):
        username = self.cleaned_data["username"]
        try:
            user = User.objects.get(username__iexact=username)
        except User.DoesNotExist:
            if is_valid_username(username):
                return username
            else:
                raise forms.ValidationError("Invalid username")
        raise forms.ValidationError("Username in use")

    def clean_email(self):
        email = self.cleaned_data['email']
        try:
            user = User.objects.get(email=email)
        except User.DoesNotExist:
            return email
        raise forms.ValidationError("Email in use")

更新

我将问题缩小到我正在使用的Javascript validator。验证者花5秒钟来完成它的工作是否正常?这是一个指示我以某种方式使用它错了吗?还有更好的选择吗?

0 个答案:

没有答案