我得到这个问题,任何帮助将不胜感激,我得到一个错误试图登录或注册。错误below. AttributeError at /sign-up 'WSGIRequest'对象没有属性'is_ AJAX '我知道该功能现在折旧,但我似乎不能解决这个问题。
mixins.py
class AjaxFormMixin(object):
'''
Mixin to ajaxify django form - can be over written in view by calling form_valid method
'''
def form_invalid(self, form):
response = super(AjaxFormMixin, self).form_invalid(form)
if self.request.is_ajax():
message = FormErrors(form)
return JsonResponse({'result': 'Error', 'message': message})
return response
def form_valid(self, form):
response = super(AjaxFormMixin, self).form_valid(form)
if self.request.is_ajax():
form.save()
return JsonResponse({'result': 'Success', 'message': ""})
return response
views.py
def profile_view(request):
'''
function view to allow users to update their profile
'''
user = request.user
up = user.userprofile
form = UserProfileForm(instance=up)
if request.is_ajax():
form = UserProfileForm(data=request.POST, instance=up)
if form.is_valid():
obj = form.save()
obj.has_profile = True
obj.save()
result = "Success"
message = "Your profile has been updated"
else:
message = FormErrors(form)
data = {'result': result, 'message': message}
return JsonResponse(data)
else:
context = {'form': form}
context['google_api_key'] = settings.GOOGLE_API_KEY
context['base_country'] = settings.BASE_COUNTRY
return render(request, 'users/profile.html', context)
class SignUpView(AjaxFormMixin, FormView):
'''
Generic FormView with our mixin for user sign-up with reCAPTURE security
'''
template_name = "users/sign_up.html"
form_class = UserForm
success_url = "/"
# reCAPTURE key required in context
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["recaptcha_site_key"] = settings.RECAPTCHA_PUBLIC_KEY
return context
# over write the mixin logic to get, check and save reCAPTURE score
def form_valid(self, form):
response = super(AjaxFormMixin, self).form_valid(form)
if self.request.is_ajax():
token = form.cleaned_data.get('token')
captcha = reCAPTCHAValidation(token)
if captcha["success"]:
obj = form.save()
obj.email = obj.username
obj.save()
up = obj.userprofile
up.captcha_score = float(captcha["score"])
up.save()
login(self.request, obj,
backend='django.contrib.auth.backends.ModelBackend')
# change result & message on success
result = "Success"
message = "Thank you for signing up"
data = {'result': result, 'message': message}
return JsonResponse(data)
return response
class SignInView(AjaxFormMixin, FormView):
'''
Generic FormView with our mixin for user sign-in
'''
template_name = "users/sign_in.html"
form_class = AuthForm
success_url = "/"
def form_valid(self, form):
response = super(AjaxFormMixin, self).form_valid(form)
if self.request.is_ajax():
username = form.cleaned_data.get('username')
password = form.cleaned_data.get('password')
# attempt to authenticate user
user = authenticate(
self.request, username=username, password=password)
if user is not None:
login(self.request, user,
backend='django.contrib.auth.backends.ModelBackend')
result = "Success"
message = 'You are now logged in'
else:
message = FormErrors(form)
data = {'result': result, 'message': message}
return JsonResponse(data)
return response
我知道有一个post with similar issue,但我有点挣扎,以修复我的结束。x1c 0d1x
2条答案
按热度按时间alen0pnh1#
在任何地方都可以像
if request.headers.get('x-requested-with') == 'XMLHttpRequest':
一样使用它,这样:对于基于类的视图,使用
if self.request.headers.get('x-requested-with') == 'XMLHttpRequest':
sdnqo3pr2#
从django-3.1开始,**
.is_ajax()
**方法[Django-doc]被弃用。实际上,在发行说明中,我们看到:HttpRequest.is_ajax()
方法已被弃用,因为它依赖于特定于jQuery的方式来表示 AJAX 调用,而当前的用法倾向于使用JavaScript Fetch API。根据您的用例,您可以编写自己的AJAX检测方法,或者如果您的代码依赖于客户端Accept HTTP头,则使用新的HttpRequest.accepts()
方法。最初,它使用:
但请注意,这是而且一直是jQuery特有的,因此没有多大意义:浏览器或HTTP库总是可以模仿这种行为,您可以在没有这个头的情况下发出 AJAX 请求,因此它是不可靠的。
您可以检查浏览器是否接受带有**
.accepts(…)
**[Django-doc]的json/xml,这可能是 AJAX 请求尝试接受的内容,因此:或:
很可能是这个的候选者。