[英]Method Not Allowed (POST) Django 405 error
I've just changed my code from function based views to class based views and now am getting an error I can't seem to resolve.我刚刚将代码从基于函数的视图更改为基于类的视图,现在遇到了一个我似乎无法解决的错误。
The error appears when a user presses a button to submit their location coordinates.当用户按下按钮提交他们的位置坐标时会出现错误。
Method Not Allowed: /connect/post [2019/02/11 14:27:17] HTTP POST /connect/post 405 [0.00, 127.0.0.1:57896]
Everything was working before and I can't figure out what I am doing wrong.之前一切正常,我无法弄清楚我做错了什么。 I have both the get and post
requests.我有get and post
请求。 Could someone point me in the right direction?有人能指出我正确的方向吗?
views.py视图.py
class ConnectView(View):
template_name = 'connect/home.html'
def get(self, request, *args, **kwargs):
context = {
'users': User.objects.exclude(username=request.user),
}
return render(request, self.template_name, context)
def post(self, request, *args, **kwargs):
location = Location(latitude=request.POST['latitude'],
longitude=request.POST['longitude'], user = request.user)
location.save()
return JsonResponse({'message': 'success'})
urls.py网址.py
urlpatterns = [
path('', connect_views.ConnectView.as_view(), name='connect_home'),
]
connect.html连接.html
<script>
function showPosition(position) {
pos = position;
var { latitude, longitude } = pos.coords;
$('#btn_submit').attr("disabled", null);
}
$(document).ready(function() {
$demo = $("#demo");
$('#btn_submit').on('click', function() {
var data = pos.coords;
data.csrfmiddlewaretoken = $('input[name=csrfmiddlewaretoken]').val();
$.post("post", data, function() {
alert("Location Confirmed!");
});
});
});
</script>
---omitted irrelevant code--
<button type="submit" id="btn_submit" class="btn btn-success" disabled>2. Confirm Location </button>
You probably are posting to the wrong URL ( connect/post/
instead of connect/
).您可能发布到错误的 URL( connect/post/
而不是connect/
)。 This question has some pointers to check for usual causes of this error.这个问题有一些指针可以检查这个错误的常见原因。
You could try by changing this line您可以尝试更改此行
$.post("post", data, function() {
to到
$.post(window.location, data, function() {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.