POST 方法返回方法不允许 405

huangapple go评论106阅读模式
英文:

NGINX Django DRF Vue.js POST method returns Method Get not allowed 405

问题

I am building an ecommerce site. Everything works well, except the forms. When I try to post something, it returns me 405 method get not allowed. Why is it giving GET error when I am trying to do POST? It works on some forms, like for example checkout. But when I try to use contact form and press send axios.post('/api/v1/contacto/', data) it gets me this error 405. BTW, when I am using this e-commerce running it on my local machine, it works well.

Here is my sites-available:

  1. upstream ecologic_app_server {
  2. server unix:/webapps/ecologic/backend/venv/run/gunicorn.sock fail_timeout=0;
  3. }
  4. server {
  5. listen 8000;
  6. listen [::]:8000;
  7. server_name myiphere;
  8. client_max_body_size 40M;
  9. location / {
  10. root /webapps/ecologic/frontend;
  11. try_files $uri /index.html;
  12. index index.html index.htm;
  13. }
  14. location /static/ {
  15. root /webapps/ecologic/backend;
  16. }
  17. location /media/ {
  18. root /webapps/ecologic/backend;
  19. }
  20. location /api/v1/ {
  21. proxy_set_header X-Real-IP $remote_addr;
  22. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  23. proxy_set_header X-NginX-Proxy true;
  24. proxy_set_header Host $http_host;
  25. proxy_pass http://ecologic_app_server/api/v1/;
  26. proxy_ssl_session_reuse off;
  27. proxy_redirect off;
  28. }
  29. location /admin/ {
  30. proxy_set_header X-Real-IP $remote_addr;
  31. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  32. proxy_set_header X-NginX-Proxy true;
  33. proxy_pass http://ecologic_app_server/admin/;
  34. proxy_ssl_session_reuse off;
  35. proxy_set_header Host $http_host;
  36. proxy_redirect off;
  37. }
  38. }

This is my urls.py:

  1. from django.urls import path, include
  2. from . import views
  3. urlpatterns = [
  4. path('contacto/', views.contact_form_post),
  5. path('reclamo/', views.complaints_form_post),
  6. ]

Here is my code for contact form:

  1. @api_view(['POST'])
  2. def contact_form_post(request):
  3. if request.method == "POST":
  4. serializer = ContactForm(data=request.data)
  5. if serializer.is_valid():
  6. first_name = serializer.validated_data['first_name']
  7. last_name = serializer.validated_data['last_name']
  8. phone = serializer.validated_data['phone']
  9. email = serializer.validated_data['email']
  10. subject = serializer.validated_data['subject']
  11. message = serializer.validated_data['message']
  12. print(first_name, last_name, phone, email, subject, message)
  13. context = {
  14. 'first_name': first_name,
  15. 'last_name': last_name,
  16. 'phone': phone,
  17. 'email': email,
  18. 'subject': subject,
  19. 'message': message
  20. }
  21. html = render_to_string('emails/contact.html', context)
  22. text = render_to_string('emails/contact.txt', context)
  23. recipient = MainSettings.objects.first().contact_email
  24. send_mail(
  25. subject,
  26. message=text,
  27. html_message=html,
  28. from_email=settings.DEFAULT_FROM_EMAIL,
  29. recipient_list=[recipient],
  30. fail_silently=False,
  31. )
  32. serializer.save()
  33. return Response(serializer.data, status=status.HTTP_201_CREATED)
  34. return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

What could be the problem? Thank you.

英文:

I am building an ecommerce site. Everything works well, except the forms. When I try to post something, it returns me 405 method get not allowed. Why is it giving GET error when I am trying to do POST? It works on some forms, like for example checkout. But when I try to use contact form and press send axios.post('/api/v1/contacto/', data) it gets me this error 405. BTW, when I am using this e-commerce running it on my local machine, it works well.

Here is my sites-available:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. upstream ecologic_app_server {
  2. server unix:/webapps/ecologic/backend/venv/run/gunicorn.sock fail_timeout=0;
  3. }
  4. server {
  5. listen 8000;
  6. listen [::]:8000;
  7. server_name myiphere;
  8. client_max_body_size 40M;
  9. location / {
  10. root /webapps/ecologic/frontend;
  11. try_files $uri /index.html;
  12. index index.html index.htm;
  13. }
  14. location /static/ {
  15. root /webapps/ecologic/backend;
  16. }
  17. location /media/ {
  18. root /webapps/ecologic/backend;
  19. }
  20. location /api/v1/ {
  21. proxy_set_header X-Real-IP $remote_addr;
  22. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  23. proxy_set_header X-NginX-Proxy true;
  24. proxy_set_header Host $http_host;
  25. proxy_pass http://ecologic_app_server/api/v1/;
  26. proxy_ssl_session_reuse off;
  27. proxy_redirect off;
  28. }
  29. location /admin/ {
  30. proxy_set_header X-Real-IP $remote_addr;
  31. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  32. proxy_set_header X-NginX-Proxy true;
  33. proxy_pass http://ecologic_app_server/admin/;
  34. proxy_ssl_session_reuse off;
  35. proxy_set_header Host $http_host;
  36. proxy_redirect off;
  37. }
  38. }

<!-- end snippet -->

This is my urls.py:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. from django.urls import path, include
  2. from . import views
  3. urlpatterns = [
  4. path(&#39;contacto/&#39;, views.contact_form_post),
  5. path(&#39;reclamo/&#39;, views.complaints_form_post),
  6. ]

<!-- end snippet -->

Here is my code for contact form:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. @api_view([&#39;POST&#39;])
  2. def contact_form_post(request):
  3. if request.method == &quot;POST&quot;:
  4. serializer = ContactForm(data=request.data)
  5. if serializer.is_valid():
  6. first_name = serializer.validated_data[&#39;first_name&#39;]
  7. last_name = serializer.validated_data[&#39;last_name&#39;]
  8. phone = serializer.validated_data[&#39;phone&#39;]
  9. email = serializer.validated_data[&#39;email&#39;]
  10. subject = serializer.validated_data[&#39;subject&#39;]
  11. message = serializer.validated_data[&#39;message&#39;]
  12. print(first_name, last_name, phone, email, subject, message)
  13. context = {
  14. &#39;first_name&#39;: first_name,
  15. &#39;last_name&#39;: last_name,
  16. &#39;phone&#39;: phone,
  17. &#39;email&#39;: email,
  18. &#39;subject&#39;: subject,
  19. &#39;message&#39;: message
  20. }
  21. html = render_to_string(&#39;emails/contact.html&#39;, context)
  22. text = render_to_string(&#39;emails/contact.txt&#39;, context)
  23. recipient = MainSettings.objects.first().contact_email
  24. send_mail(
  25. subject,
  26. message=text,
  27. html_message=html,
  28. from_email=settings.DEFAULT_FROM_EMAIL,
  29. recipient_list=[recipient],
  30. fail_silently=False,
  31. # auth_user=None, auth_password=None, connection=None, html_message=None
  32. )
  33. serializer.save()
  34. return Response(serializer.data, status=status.HTTP_201_CREATED)
  35. return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

<!-- end snippet -->

What could be the problem? Thank you.

答案1

得分: 1

这个问题可能是一个Nginx配置问题。

这个问题相关,可能会有所帮助:

https://stackoverflow.com/questions/24415376/post-request-not-allowed-405-not-allowed-nginx-even-with-headers-included

关于它在你本地运行正常,请将你的应用程序Docker化。

英文:

I suppose that its an Nginx config problem.

This question is related and may help:

https://stackoverflow.com/questions/24415376/post-request-not-allowed-405-not-allowed-nginx-even-with-headers-included

Regarding that it works on your local, please Dockerize your app.

答案2

得分: 0

我已找到此问题的解决方案。由于某种原因,即使您无需登录或注册即可发送联系电子邮件,但不允许使用POST方法。我的Django后端应用程序的settings.py设置是正确的,我没有在那里指定任何与身份验证相关的内容。我通过将AllowAny添加到使用POST方法的视图中来解决了这个问题。在您的views.py中首先添加:

  1. from rest_framework.permissions import AllowAny

然后,您添加:

  1. @api_view(['POST'])
  2. @permission_classes([AllowAny])
  3. def contact(request):

所以,我不确定为什么它会以这种方式工作。即使在那之后,我尝试删除它,也可以正常工作,而不需要AllowAny。

英文:

I have found a solution for this case. For some reason, POST methods were not allowed even though you didn't have to log in o register to send a contact email. The settings.py of my Django backend app was good. I didn't specify there anything related to being authenticated. I was able to solve that by adding AllowAny to my views that use POST method. In your views.py first add:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. from rest_framework.permissions import AllowAny

<!-- end snippet -->

Then, you add:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. @api_view([&#39;POST&#39;])
  2. @permission_classes([AllowAny])
  3. def contact(request):

<!-- end snippet -->

So, I am not sure why this is working that way. Even after that, I tried deleting it and it worked well without AllowAny.

huangapple
  • 本文由 发表于 2023年6月1日 01:56:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76376165.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定