简体   繁体   English

如何对 django 中的 object 数组进行排序?

[英]how to sort an array of object in django?

I am making an project with python and django about github profile.我正在制作一个关于 github 配置文件的 python 和 django 项目。 I already successful call user github API and it give me like a lot of repos from that user and I want to display like 8 of them and sorted by stars or forks or size of that repo.我已经成功调用了用户 github API,它给了我来自该用户的很多存储库,我想显示其中的 8 个,并按星号或分叉或该存储库的大小排序。 How can I do that?我怎样才能做到这一点?

Here's an image of my page:这是我的页面的图像: 在此处输入图像描述

Here is my views.py :这是我的views.py

def user(req, username):
    username = str.lower(username)

    # Get User Info

    with urlopen(f'https://api.github.com/users/{username}') as response:
        source = response.read()
    data = json.loads(source)

    # Get Limit Call API

    with urlopen(f'https://api.github.com/rate_limit') as response:
        source = response.read()
    limit_data = json.loads(source)

    # Get User Repo Info
    with urlopen(f'https://api.github.com/users/{username}/repos') as response:
        source = response.read()
    user_repos = json.loads(source)

    def sort_user_repo_by_stars(user_repos):
        return user_repos['stargazers_count']

    user_repos.sort(key=sort_user_repo_by_stars, reverse=True)

    created_at = data['created_at']
    created_at = datetime.datetime.strptime(created_at, "%Y-%m-%dT%H:%M:%SZ")
    created_at = created_at.strftime("%B %d, %Y")

    context = {
        'username': username,
        'data': data,
        'created_at': created_at,
        'limit_data': limit_data,
        'user_repos': user_repos,
    }
    return render(req, 'user.html', context)

and here is my template user.html :这是我的模板user.html

<div class="repos">
    <div class="top-repo">
        <label for="top-repos" class="col-sm-3 col-form-label">Top Repos <span>by </span></label>
        <select class="custom-select bg-light text-primary" name="pick">
            <option selected="stars">stars</option>
            <option value="forks">forks</option>
            <option value="size">size</option>
        </select>
    </div>
    <div class="repo-info">
        {% for repo in user_repos %}
        <div class="card-deck">
            <div class="card shadow">
                <div class="card-body">
                    <h4 class="card-title">{{repo.name}}</h4>
                    <p class="card-text clearfix">
                        <i class="fas fa-circle"></i> {{repo.language}}
                        <i class="fas fa-star"></i> {{repo.stargazers_count}}
                        <i class="fal fa-code-branch"></i> {{repo.forks}}
                        <span class="float-right">{{repo.size}} KB</span>
                    </p>
                </div>
            </div>
        </div>
        {% endfor %}
    </div>
</div>

If you only want an array of 8 elements then you can use a slice operator on your list.如果您只想要一个包含 8 个元素的数组,那么您可以在列表中使用切片运算符。

context = {
    "username": username,
    "data": data,
    "created_at": created_at,
    "limit_data": limit_data,
    "user_repos": user_repos[:8],
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM