Django-错误的请求语法或不受支持的方法

时间:2019-12-05 14:50:56

标签: django

我想通过api将数据发布到我的Django应用中。这就是我走的距离:

import pandas
import requests 

excel_data_df = pandas.read_excel('workorders.xlsx')
json_str = excel_data_df.to_json(orient='records', date_format='iso')

API_ENDPOINT = "http://127.0.0.1:8000/api/create/"
API_KEY = "dF8NbXRA.94Mj2xeXT3NZOtx1b575CvNvbs8JWo0D"
data = {'api_dev_key':API_KEY, 
        'api_option':'paste', 
        'api_paste_code':json_str , 
        'api_paste_format':'csv'} 

r = requests.post(url = API_ENDPOINT, data = data) 

Django


views.py

class PostDataView(CreateAPIView):
    queryset = Workorder.objects.all()
    serializer_class = WorkorderSerializer

serializers.py

class WorkorderSerializer(serializers.ModelSerializer):

    class Meta:
        model = Workorder
        exclude = ['id']

urls.py

from django.conf.urls import url, include
from .views import *
from rest_framework.urlpatterns import format_suffix_patterns
from django.conf import settings
from django.conf.urls.static import static


urlpatterns = [

    url(r'^api/chart/data/$', ChartData.as_view(),name="api-data"),
    url(r'^api/create/$', PostDataView.as_view(), name='create'),
    url(r'^(?P<pk>\d+)/api/delete/$', DeleteDataView.as_view(), name='delete'),
    url(r'^(?P<pk>\d+)/api/update/$', UpdateDataView.as_view(), name='update'),
    url(r'^$', display_mobiles, name="display_mobiles"), 
    url(r'^(?P<pk>\d+)$', edit_mobile, name="edit_mobile"),
    url(r'^delete/(?P<pk>\d+)$', delete_mobile, name="delete_mobile"),
    url(r'^home/$', index, name='index'),
    url(r'^sitemap/$', sitemap, name='sitemap'),
    url(r'^upload/$', upload, name='upload'),
    url(r'^test/$', testview, name='test')
]

if settings.DEBUG: 
    urlpatterns = urlpatterns + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    urlpatterns = urlpatterns + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

urlpatterns = format_suffix_patterns(urlpatterns)

models.py

class Workorder(models.Model):
    label                       = models.CharField(max_length=200, blank=False)
    start                       = models.DateField() 
    end                         = models.DateField() 
    duration                    = models.IntegerField() 
    ctype                       = models.CharField(max_length=20, default='bar')                                                                 
    werk                        = models.CharField(max_length=50, default='plant')
    product                     = models.CharField(max_length=50) 
    train_number                = models.IntegerField()
    latest_start_date           = models.DateField()
    latest_start_timebucket     = models.IntegerField()
    is_start_date_fixed         = models.BooleanField()
    assigned_start_timebucket   = models.IntegerField()
    assigned_start_date         = models.DateField()
    costs_early_start           = models.IntegerField()
    costs_late_start            = models.IntegerField()
    resource_overall_demands    = models.IntegerField()
    resource_timeslots_demands  = models.IntegerField() 

我可以使用发布表格@ api / create手动输入数据,但是当我尝试通过api发布数据时,出现此错误:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
        <title>Error response</title>
    </head>
    <body>
        <h1>Error response</h1>
        <p>Error code: 400</p>
        <p>Message: Bad Request.</p>
        <p>Error code explanation: 400 - Bad request syntax or unsupported method.</p>
    </body>
</html>

这是我尝试发布的数据:

1)

[
{"id":null,"label":"Workorder 1","start":"2019-01-01T00:00:00.000Z","end":"2019-01-10T00:00:00.000Z","duration":9,"ctype":"bar","werk":"a","product":"a","train_number":535435.0,"latest_start_date":"2019-01-10T00:00:00.000Z","latest_start_timebucket":9,"is_start_date_fixed":false,"assigned_start_timebucket":535435.0,"assigned_start_date":"2019-01-10T00:00:00.000Z","costs_early_start":334,"costs_late_start":334,"resource_overall_demands":334,"resource_timeslots_demands":334},
{"id":null,"label":"Workorder 1","start":"2019-01-01T00:00:00.000Z","end":"2019-01-10T00:00:00.000Z","duration":9,"ctype":"bar","werk":"a","product":"a","train_number":535435.0,"latest_start_date":"2019-01-10T00:00:00.000Z","latest_start_timebucket":9,"is_start_date_fixed":false,"assigned_start_timebucket":535435.0,"assigned_start_date":"2019-01-10T00:00:00.000Z","costs_early_start":334,"costs_late_start":334,"resource_overall_demands":334,"resource_timeslots_demands":334}
]

2)

[
{"id":null,"label":"Workorder 1","start":"2019-01-01T00:00:00.000Z","end":"2019-01-10T00:00:00.000Z","duration":9,"ctype":"bar","werk":"a","product":"a","train_number":535435,"latest_start_date":"2019-01-10T00:00:00.000Z","latest_start_timebucket":9,"is_start_date_fixed":false,"assigned_start_timebucket":535435,"assigned_start_date":"2019-01-10T00:00:00.000Z","costs_early_start":334,"costs_late_start":334,"resource_overall_demands":334,"resource_timeslots_demands":334}
]

3)

{"id":null,"label":"Workorder 1","start":"2019-01-01T00:00:00.000Z","end":"2019-01-10T00:00:00.000Z","duration":9,"ctype":"bar","werk":"a","product":"a","train_number":535435,"latest_start_date":"2019-01-10T00:00:00.000Z","latest_start_timebucket":9,"is_start_date_fixed":false,"assigned_start_timebucket":535435,"assigned_start_date":"2019-01-10T00:00:00.000Z","costs_early_start":334,"costs_late_start":334,"resource_overall_demands":334,"resource_timeslots_demands":334}

4)

{"id":1}

谢谢您的帮助

2 个答案:

答案 0 :(得分:1)

执行requests.post(url = API_ENDPOINT, data = data)时,它会将yoru数据作为表单数据发送。

您似乎正在尝试以API不接受的格式发送数据。

我认为您的API已配置为仅接受JSON,但您正尝试将其作为表单数据发送。

https://www.django-rest-framework.org/api-guide/parsers/#setting-the-parsers的文档中检查设置

您还可以尝试使用requests.post(..., json=payload)将数据作为JSON字符串发布。参考https://requests.readthedocs.io/en/master/user/quickstart/#more-complicated-post-requests

答案 1 :(得分:0)

我不明白api_...指的是什么。

但是鉴于错误消息,您的客户端似乎没有发送适当的请求。

一种有用的调试方法是尝试使其首先与devtools中生成的代码一起使用:

  • 从DRF转到可浏览的API
  • 打开devtools,转到Network,选择All
  • 通过可浏览的API进行POST
  • 在devtools中找到相应的请求,右键单击它,然后选择copy as fetchcopy as curl
  • 然后尝试在浏览器(用于获取)或bash(用于curl)中运行它,并查看其是否有效。