问题
I have created a very simple form submission for users to register, requiring them to enter their email, username, and password. There is a ~5 sec delay from when I click the submit button to when the form actually submits. How can I figure out what's going on here? Here is what I have tried so far:
Django Debug Toolbar - Profiling:
It seems that there are clues here, but I have been unable to use this information to solve the issue using this data. Any ideas? Profiling Image
Javascript
I attached some progress bar Javascript using NProgress. It is interesting to note that the progress bar does not start until after the 5 sec delay. Here's the HTML (orange-bt class is just formatting):
<form class="register-form" method="POST" action="/register/">
{% csrf_token %}
{{ form.as_p }}
<input class="progress-bar orange-bt" type="submit" value="Register">
</form>
And some Javascript at the bottom:
<script>
$(".progress-bar").click(function() { NProgress.start(); });
</script>
views.py
I inserted a few print to console lines while troubleshooting, and it appears that the delay occurs right before the if request.method == 'POST' line. All the actions in the POST if statement occur quickly enough after the 5 sec delay, but I cannot figure out what is holding up their execution for these 5 sec. Its driving me nuts!
@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
Here's the forms.py. Could the validation process be causing the delay?
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")
UPDATE
I narrowed the issue down to this Javascript validator that I am using. Is it normal for a validator to take 5 sec to do it's job? Is this an indicator that I am somehow using it wrong? Are there better alternatives?
来源:https://stackoverflow.com/questions/39476572/troubleshooting-5-sec-delay-in-form-submission