Merge remote-tracking branch 'LCTT/master'

This commit is contained in:
Xingyu Wang 2020-01-05 21:58:57 +08:00
commit 6613419efb
6 changed files with 249 additions and 264 deletions

View File

@ -1,39 +1,39 @@
[#]: collector: (lujun9972)
[#]: translator: (wxy)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: reviewer: (wxy)
[#]: publisher: (wxy)
[#]: url: (https://linux.cn/article-11751-1.html)
[#]: subject: (12 programming resources for coders of all levels)
[#]: via: (https://opensource.com/article/19/12/programming-resources)
[#]: author: (Erik O'Shaughnessy https://opensource.com/users/jnyjny)
12 个给码农们的编程资源
12 个给全等级码农们的编程资源
======
> 无论你身处编程旅程中的何处,这 12 篇编程文章都有你需要学习一些东西。
![Woman sitting in front of her computer][1]
![](https://img.linux.net.cn/data/attachment/album/202001/05/102002cdjy66jucbcfs6dg.jpg)
> “学习计算机编程的最佳时间是 20 年前,其次是现在。”
> — (也许是)计算机科学的谚语
> — 计算机科学的谚语(也许是)
你是新程序员、经验丰富的 Web 开发人员、后端大师、头发花白的系统程序员,还是其他神秘物种的计算机极客?无论你身处广阔的软件开发人员生态系统中的何处,在我们领域中唯一不变的就是需要了解新技术及其应用方法。这是过去一年来阅读量最大的 Opensource.com 文章的集合,我很高兴在这里与你分享它们。
无论你是新程序员、经验丰富的 Web 开发人员、后端大师、头发花白的系统程序员,还是其他神秘物种的计算机极客?无论你身处广阔的软件开发人员生态系统中的何处,在我们领域中唯一不变的就是需要了解新技术及其应用方法。这是过去一年来阅读量最大的 Opensource.com 文章的集合,我很高兴在这里与你分享它们。
### 好好学习
无论你的技能或兴趣是什么,学习如何编写计算机程序都可以将特定领域的知识变成一种超能力。不幸的是,至少从现在开始,还不可能通过坐在计算机前面接受电磁辐射就能学会如何编程。在这之前,我推荐这些出色的文章来帮助激发你的编程技能。它们解决了一些重要的问题,例如考虑学习哪种语言以及不同的入门方式。
* [你应该学习哪种编程语言?][2]
* [学习 Python 的 12 个方式][3]
* [你可以使用树莓派学习的三种流行编程语言][4]
* [利用 Python 引导孩子的计算机思维][5]
* [你应该学习哪种编程语言?][2](译文)
* [学习 Python 的 12 个方式][3](译文)
* [你可以使用树莓派学习的三种流行编程语言][4](译文)
* [利用 Python 引导孩子的计算机思维][5](译文)
### 天天向上
经验丰富的程序员知道,与学习编程相比,唯一更难的就是使你的技能和知识保持最新。但是我们是[自学成才][6]的一族,总是希望提高我们的专业知识和理解力。即使我们不期望使用新技术,我们也知道总会有人问起它们。这些有关 Rust、WebAssembly 和 Podman 的文章是开始学习软件技术一些即将出现的趋势的好地方。
* [为什么要在 WebAssembly 中使用 Rust][7]
* [使用 WebAssembly 提升命令行游乐场][8]
* [使用 WebAssembly 扩展命令行领域][8]
* [免 root 的 Podman 如何工作?][9]
* [为什么选择 Rust 作为你的下一种编程语言][10]
@ -42,8 +42,8 @@
尽管新技术层出不穷,但回顾过去可以帮助你前进。当今惊人的技术是建立在昨天的工具之上的,即使我们从未使用这些传奇语言编写过代码,它也将使我们很好地理解这些工具的功能和局限性。这一系列文章集中于 C 语言、用 AWK 编写的实际应用程序,以及对流行度下降但也许还没有消亡的计算机语言的有趣讨论。
* [C 的巨大影响] [11]
* [如何写好 C main 函数][12]
* [用 AWK 喝咖啡][13]
* [如何写好 C main 函数][12](译文)
* [用 AWK 喝咖啡][13](译文)
* [你最喜欢的“死”语言是什么?][14]
### 学习不止
@ -57,7 +57,7 @@ via: https://opensource.com/article/19/12/programming-resources
作者:[Erik O'Shaughnessy][a]
选题:[lujun9972][b]
译者:[wxy](https://github.com/wxy)
校对:[校对者ID](https://github.com/校对者ID)
校对:[wxy](https://github.com/wxy)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出

View File

@ -1,3 +1,5 @@
summer2233 is translating
Excellent Business Software Alternatives For Linux
-------

View File

@ -1,247 +0,0 @@
[#]: collector: (lujun9972)
[#]: translator: (MjSeven)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (How to write a Python web API with Django)
[#]: via: (https://opensource.com/article/19/11/python-web-api-django)
[#]: author: (Rachel Waston https://opensource.com/users/rachelwaston)
How to write a Python web API with Django
======
Django is one of the most popular frameworks for Python API development.
Learn how to use it in this quick tutorial.
![Hands on a keyboard with a Python book ][1]
[Django][2] is the comprehensive web framework by which all other frameworks are measured. One of the most popular names in Python API development, Django has surged in popularity since its start in 2005.
Django is maintained by the Django Software Foundation and has experienced great community support, with over 11,600 members worldwide. On Stack Overflow, Django has around 191,000 tagged questions. Websites like Spotify, YouTube, and Instagram rely on Django for application and data management.
This article demonstrates a simple API to fetch data from a server using the GET method of the HTTP protocol.
### Set up a project
First, create a structure for your Django application; you can do this at any location on your system:
```
$ mkdir myproject
$ cd myproject
```
Then, create a virtual environment to isolate package dependencies locally within the project directory:
```
$ python3 -m venv env
$ source env/bin/activate
```
On Windows, use the command **env\Scripts\activate** to activate your Python virtual environment.
### Install Django and the Django REST framework
Next, install the Python modules for Django and Django REST:
```
$ pip3 install django
$ pip3 install djangorestframework
```
### Instantiate a new Django project
Now that you have a work environment for your app, you must instantiate a new Django project. Unlike a minimal framework like [Flask][3], Django includes dedicated commands for this process (note the trailing **.** character in the first command):
```
$ django-admin startproject tutorial .
$ cd tutorial
$ django-admin startapp quickstart
```
Django uses a database as its backend, so you should sync your database before beginning development. The database can be managed with the **manage.py** script that was created when you ran the **django-admin** command. Because you're currently in the **tutorial** directory, use the **../** notation to run the script, located one directory up:
```
$ python3 ../manage.py makemigrations
No changes detected
$ python4 ../manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying sessions.0001_initial... OK
```
### Create users in Django
Create an initial user named **admin** with the example password of **password123**:
```
$ python3 ../manage.py createsuperuser \
  --email [admin@example.com][4] \
  --username admin
```
Create a password when you're prompted.
### Implement serializers and views in Django
For Django to be able to pass information over to an HTTP GET request, the information object must be translated into valid response data. Django implements **serializers** for this.
In your project, define some serializers by creating a new module named **quickstart/serializers.py**, which you'll use for data representations:
```
from django.contrib.auth.models import User, Group
from rest_framework import serializers
class UserSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = User
        fields = ['url', 'username', 'email', 'groups']
class GroupSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Group
        fields = ['url', 'name']
```
A [view][5] in Django is a function that takes a web request and returns a web response. The response can be HTML, or an HTTP redirect, or an HTTP error, a JSON or XML document, an image or TAR file, or anything else you can get over the internet. To create a view, open **quickstart/views.py** and enter the following code. This file already exists and has some boilerplate text in it, so keep that and append this text to the file:
```
from django.contrib.auth.models import User, Group
from rest_framework import viewsets
from tutorial.quickstart.serializers import UserSerializer, GroupSerializer
class UserViewSet(viewsets.ModelViewSet):
    """
    API endpoint  allows users to be viewed or edited.
    """
    queryset = User.objects.all().order_by('-date_joined')
    serializer_class = UserSerializer
class GroupViewSet(viewsets.ModelViewSet):
    """
    API endpoint  allows groups to be viewed or edited.
    """
    queryset = Group.objects.all()
    serializer_class = GroupSerializer
```
### Generate URLs with Django
Now you can generate URLs so people can access your fledgling API. Open **urls.py** in a text editor and replace the default sample code with this code:
```
from django.urls import include, path
from rest_framework import routers
from tutorial.quickstart import views
router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
router.register(r'groups', views.GroupViewSet)
# Use automatic URL routing
# Can also include login URLs for the browsable API
urlpatterns = [
    path('', include(router.urls)),
    path('api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]
```
### Adjust your Django project settings
The settings module for this example project is stored in **tutorial/settings.py**, so open that in a text editor and add **rest_framework** to the end of the **INSTALLED_APPS** list:
```
INSTALLED_APPS = [
    ...
    'rest_framework',
]
```
### Test your Django API
You're now ready to test the API you've built. First, start up the built-in server from the command line:
```
`$ python3 manage.py runserver`
```
You can access your API by navigating to the URL **<http://localhost:8000/users>** using **curl**:
```
$ curl --get <http://localhost:8000/users/?format=json>
[{"url":"<http://localhost:8000/users/1/?format=json","username":"admin","email":"admin@example.com","groups":\[\]}\]>
```
Or use Firefox or the [open source web browser][6] of your choice:
![A simple Django API][7]
For more in-depth knowledge about RESTful APIs using Django and Python, see the excellent [Django documentation][8].
### Why should I use Django?
The major benefits of Django:
1. The size of the Django community is ever-growing, so you have lots of resources for guidance, even on a complicated project.
2. Features like templating, routing, forms, authentication, and management tools are included by default. You don't have to hunt for external tools or worry about third-party tools introducing compatibility issues.
3. Simple constructs for users, loops, and conditions allow you to focus on writing code.
4. It's a mature and optimized framework that is extremely fast and reliable.
The major drawbacks of Django are:
1. Django is complex! From a developer's point of view, Django can be trickier to learn than a simpler framework.
2. There's a big ecosystem around Django. This is great once you're comfortable with Django, but it can be overwhelming when you're still learning.
Django is a great option for your application or API. Download it, get familiar with it, and start developing an amazing project!
--------------------------------------------------------------------------------
via: https://opensource.com/article/19/11/python-web-api-django
作者:[Rachel Waston][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/rachelwaston
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/python-programming-code-keyboard.png?itok=fxiSpmnd (Hands on a keyboard with a Python book )
[2]: https://www.djangoproject.com/
[3]: https://opensource.com/article/19/11/python-web-api-flask
[4]: mailto:admin@example.com
[5]: https://docs.djangoproject.com/en/2.2/topics/http/views/
[6]: https://opensource.com/article/19/7/open-source-browsers
[7]: https://opensource.com/sites/default/files/uploads/django-api.png (A simple Django API)
[8]: https://docs.djangoproject.com/en/2.2

View File

@ -1,5 +1,5 @@
[#]: collector: (lujun9972)
[#]: translator: ( )
[#]: translator: (lxbwolf)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )

View File

@ -1,5 +1,5 @@
[#]: collector: (lujun9972)
[#]: translator: ( )
[#]: translator: (summer2233)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )

View File

@ -0,0 +1,230 @@
[#]: collector: (lujun9972)
[#]: translator: (MjSeven)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (How to write a Python web API with Django)
[#]: via: (https://opensource.com/article/19/11/python-web-api-django)
[#]: author: (Rachel Waston https://opensource.com/users/rachelwaston)
如何借助 Django 来编写一个 Python Web API
======
Django 是 Python API 开发中最流行的框架之一,在这个教程中,我们来学习如何使用它。
![拿起 Python 书来学习吧][1]
[Django][2] 所有 Web 框架中最全面的,也是最受欢迎的一个。自 2005 年以来,其流行度大幅上升。
Django 是由 Django 软件基金会维护,并且获得了社区的大力支持,在全球拥有超过 11,600 名会员。在 Stack Overflow 上Django 约有 191,000 个带标签的问题。Spotify、YouTube 和 Instagram 等都使用 Django 来构建应用程序和数据管理。
本文演示了一个简单的 API通过它可以使用 HTTP 协议的 GET 方法来从服务器获取数据。
### 构建一个项目
首先,为你的 Django 应用程序创建一个目录结构,你可以在系统的任何位置创建:
```
$ mkdir myproject
$ cd myproject
```
然后,在项目目录中创建一个虚拟环境来隔离本地包依赖关系:
```
$ python3 -m venv env
$ source env/bin/activate
```
在 Windows 上,使用命令 **env\Scripts\activate** 来激活虚拟环境:
### 安装 Django 和 Django REST framework
然后,安装 Django 和 Django REST
```
$ pip3 install django
$ pip3 install djangorestframework
```
### 实例化一个新的 Django 项目
现在你的应用程序已经有了一个工作环境,你必须实例化一个新的 Django 项目。与 [Flask][3] 这样微框架不同的是Django 有专门的命令来创建(注意第一条命令后的 **.** 字符)。
```
$ django-admin startproject tutorial .
$ cd tutorial
$ django-admin startapp quickstart
```
Django 使用数据库来管理后端,所以你应该在开始开发之前同步数据库,数据库可以通过 **manage.py** 脚本管理,它是在你运行 **django-admin** 命令时创建的。因为你现在在 **tutorial** 目录,所以使用 **../** 符号来运行脚本,它位于上一层目录:
```
$ python3 ../manage.py makemigrations
No changes detected
$ python4 ../manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying sessions.0001_initial... OK
```
### 在 Django 中创建用户
创建一个名为 **admin**,密码为 **password123** 的初始用户:
```
$ python3 ../manage.py createsuperuser \
  --email [admin@example.com][4] \
  --username admin
```
在提示时创建密码。
### 在 Django 中实现序列化和视图
为了使 Django 能够将信息传递给 HTTP GET 请求必须将信息对象转化为有效的响应数据。Django 为此实现了“序列化类”。
在你的项目中,创建一个名为 **quickstart/serializers.py** 的新模块,使用它来定义一些序列化器,模块将用于数据展示:
```
from django.contrib.auth.models import User, Group
from rest_framework import serializers
class UserSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = User
        fields = ['url', 'username', 'email', 'groups']
class GroupSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Group
        fields = ['url', 'name']
```
Django 中的 [view][5] 是一个接受 Web 请求并返回 Web 响应的函数。响应可以是 HTML、HTTP 重定向、HTTP 错误、JSON 或 XML 文档、图像或 TAR 文件,或者可以是从 Internet 获得的任何其他内容。要创建视图,打开 **quickstart/views.py** 并输入以下代码。该文件已经存在,并且其中包含一些示例文本,保留这些文本并将以下代码添加到文件中:
```
from django.contrib.auth.models import User, Group
from rest_framework import viewsets
from tutorial.quickstart.serializers import UserSerializer, GroupSerializer
class UserViewSet(viewsets.ModelViewSet):
    """
    API 允许查看或编辑用户
    """
    queryset = User.objects.all().order_by('-date_joined')
    serializer_class = UserSerializer
class GroupViewSet(viewsets.ModelViewSet):
    """
    API 允许查看或编辑组
    """
    queryset = Group.objects.all()
    serializer_class = GroupSerializer
```
### 使用 Django 生成 URL
现在,你可以生成 URL 以便人们可以访问你刚起步的 API。在文本编辑器中打开 urls.py 并将默认示例代码替换为以下代码:
```
from django.urls import include, path
from rest_framework import routers
from tutorial.quickstart import views
router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
router.register(r'groups', views.GroupViewSet)
# 使用自动路由 URL
# 还有登录 URL
urlpatterns = [
    path('', include(router.urls)),
    path('api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]
```
### 调整你的 Django 项目设置
这个示例项目的设置模块存储在 **tutorial/settings.py** 中,因此在文本编辑器中将其打开,然后在 **INSTALLED_APPS** 列表的末尾添加 **rest_framework**
```
INSTALLED_APPS = [
    ...
    'rest_framework',
]
```
### 测试 Django API
现在,你可以测试构建的 API。首先从命令行启动内置服务器
```
`$ python3 manage.py runserver`
```
你可以通过使用 **curl** 导航至 URL **<http://localhost:8000/users>** 来访问 API
```
$ curl --get <http://localhost:8000/users/?format=json>
[{"url":"<http://localhost:8000/users/1/?format=json","username":"admin","email":"admin@example.com","groups":\[\]}\]>
```
使用 Firefox 或[开源浏览器][6]
![一个简单的 Django API][7]
有关使用 Django 和 Python 的 RESTful API 的更多深入知识,参考出色的 [Django 文档][8]。
### 为什么要使用 Djago
Django 的主要优点:
1. Django 社区的规模正在不断扩大,因此即使你做一个复杂项目,也会有大量的指导资源。
2. 默认包含模板、路由、表单、身份验证和管理工具等功能,你不必寻找外部工具,也不必担心第三方工具会引入兼容性问题。
3. 用户,循环和条件的简单结构使你可以专注于编写代码。
4. 这是一个成熟且经过优化的框架,它非常快速且可靠。
Django 的主要缺点是:
1. Django 很复杂!从开发人员视角的角度来看,它可能比简单的框架更难学。
2. Django 有一个很大的生态系统。一旦你熟悉它,这会很棒,但是当你深入学习时,它可能会令人感到无所适从。
对你的应用程序或 API 来说Django 是绝佳选择。下载并熟悉它,开始开发一个迷人的项目!
--------------------------------------------------------------------------------
via: https://opensource.com/article/19/11/python-web-api-django
作者:[Rachel Waston][a]
选题:[lujun9972][b]
译者:[MjSeven](https://github.com/MjSeven)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/rachelwaston
[b]: https://github.com/lujun9972
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/python-programming-code-keyboard.png?itok=fxiSpmnd (Hands on a keyboard with a Python book )
[2]: https://www.djangoproject.com/
[3]: https://opensource.com/article/19/11/python-web-api-flask
[4]: mailto:admin@example.com
[5]: https://docs.djangoproject.com/en/2.2/topics/http/views/
[6]: https://opensource.com/article/19/7/open-source-browsers
[7]: https://opensource.com/sites/default/files/uploads/django-api.png (A simple Django API)
[8]: https://docs.djangoproject.com/en/2.2