小编Rav*_*han的帖子

Django休息API,自动化文档?

我尝试在编写视图集和使用django rest docs时记录API .我遇到以下问题:

  • 如果我尝试发送反向相关字段的值,它会获取值列表,但是当在Form-data中发送数据时,它会以字符串形式出现.

  • 文档UI中没有文件上传选项.

以下是我的代码:

models.py

class Area(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    name = models.CharField(max_length=100)
    address = models.TextField()
    image = models.ImageField(upload_to='area/')
    created_on = models.DateTimeField(auto_now_add=True)
    modified_on = models.DateTimeField(auto_now=True)
    zipcode = models.CharField(max_length=15, null=True)
    is_verified = models.BooleanField(default=False)

    class Meta:
        ordering = ('-modified_on',)



class Email(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    email = models.EmailField()
    area = models.ForeignKey(Area, on_delete=models.CASCADE, null=True, related_name='email')


class Phone(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    phone = models.CharField(max_length=15)
    area = models.ForeignKey(Area, on_delete=models.CASCADE, null=True, related_name='phone')
Run Code Online (Sandbox Code Playgroud)

view.py

class AreaViewSet(viewsets.ModelViewSet):
    """ …
Run Code Online (Sandbox Code Playgroud)

python django api-doc django-rest-framework django-swagger

23
推荐指数
1
解决办法
1270
查看次数

如何在烧瓶中获得当前的基本URI?

在下面的代码中,我想将URL存储在一个变量中,以检查发生URL错误的错误.

@app.route('/flights', methods=['GET'])
def get_flight():
    flight_data= mongo.db.flight_details
    info = []
    for index in flight_data.find():
        info.append({'flight_name': index['flight_name'], 'flight_no': index['flight_no'], 'total_seat': index['total_seat'] })
    if request.headers['Accept'] == 'application/xml':
        template = render_template('data.xml', info=info)
        xml_response = make_response(template)
        xml_response.headers['Accept'] = 'application/xml'
        logger.info('sucessful got data')
        return xml_response
    elif request.headers['Accept'] == 'application/json':
        logger.info('sucessful got data')
        return jsonify(info)
Run Code Online (Sandbox Code Playgroud)

输出:**

* Restarting with stat
 * Debugger is active!
 * Debugger PIN: 165-678-508
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [28/Mar/2017 10:44:53] "GET /flights HTTP/1.1" 200 - …
Run Code Online (Sandbox Code Playgroud)

python url flask python-3.x

6
推荐指数
2
解决办法
2万
查看次数

Swagger UI中没有HEAD方法的“尝试”按钮

我有一个Swagger规范,它定义了HEAD操作:

head:
  description: show flight exist or not.

  parameters:
    - name: flight_no
      in: path
      type: string
      description: Flight_no
      required: true
  produces:
    - application/json
    - application/xml

  responses:
    200:
      description: success response
      schema:
        type: array
        items:
          $ref: '#/definitions/Data'
    '404':
      description: flight does not exist
Run Code Online (Sandbox Code Playgroud)

在Swagger UI v。2中,此HEAD操作没有“ try it out”按钮。如何为HEAD添加“试用”?

Swagger UI中的HEAD操作

swagger swagger-ui swagger-2.0

5
推荐指数
1
解决办法
1797
查看次数

os.environ.get() 不返回 Windows 中的环境值?

我已经设置了 SLACK_TOKEN 环境变量。但 "SLACK_TOKEN=os.environ.get('SLACK_TOKEN')"正在返回“无”。SLACK_TOKEN的类型是NoneType。我认为 os.environ.get 没有获取环境变量的值。所以其余的代码没有执行。

import os
from slackclient import SlackClient


SLACK_TOKEN= os.environ.get('SLACK_TOKEN') #returning None
print(SLACK_TOKEN) # None
print(type(SLACK_TOKEN)) # NoneType class

slack_client = SlackClient(SLACK_TOKEN)
print(slack_client.api_call("api.test")) #{'ok': True}
print(slack_client.api_call("auth.test")) #{'ok': False, 'error': 'not_authed'}


def list_channels():
    channels_call = slack_client.api_call("channels.list")
    if channels_call['ok']:
        return channels_call['channels']
    return None

def channel_info(channel_id):
    channel_info = slack_client.api_call("channels.info", channel=channel_id)
    if channel_info:
        return channel_info['channel']
    return None

if __name__ == '__main__':
    channels = list_channels()
    if channels:
        print("Channels: ")
        for c in channels:
            print(c['name'] + " …
Run Code Online (Sandbox Code Playgroud)

python environment-variables slack

3
推荐指数
1
解决办法
2万
查看次数