首页 > 解决方案 > 导入本地python文件时出现Attritube错误

问题描述

我需要一些帮助来解决问题。下面的代码试图导入一个名为 game_getter.py 的文件来访问它的 all_games 字典变量。

from django.db import models
from catolog import game_getter
# Create your models here.

class Game(models.Model):
    url_clue = ["//images.igdb.com"]
    for game in game_getter.all_games:
        title = game_getter.all_games[game][0]
        if url_clue in game_getter.all_games[game][1]:
            cover_art = game_getter.all_games[game]
        else:
            pass
        if game_getter.all_games[game][2] == None:
            pass
        else:
            storyline = game_getter.all_games[game][2]
        if game_getter.all_games[game][3] == None:
            pass
        else:
            storyline = game_getter.all_games[game][3]
        genre_pac = game_getter.all_games[game][4]
        
    def __str__(self):
        return self.title

class UserSearch():
    user_input = str

在下一个会话的底部,我使用了字典对象 all_games 的返回值。我什至尝试将其设为全局变量,但计算机仍然看不到它。

# Create a search
def search_query(user_input, exp_time):

    # Generate token if needed
    generate_token(exp_time)

    #establish client_id for wrapper
    client_id = "not my client_id"
    wrapper = IGDBWrapper(client_id, token)

    # Query the API based on a search term.
    query = wrapper.api_request(
                'games', # Requesting the name, storyline, genre name, and cover art url where the user input is the search tearm
                f'fields name, storyline, genres.slug, cover.url; offset 0; where name="{user_input}"*; sort first_release_date; limit: 100;',
                            # Also sorted by release date with a limit of 100 results
                )
    # Load the binary data into json
    message_json = json.loads(query)
    
    # List of all games returned
    global all_games
    all_games = dict()
    key = 0

    # Grab each value by key and separate per game
    for game in message_json:
        name = game.get('name')
        cover_url = game.get('cover')
        storyline = game.get('storyline')
        summary = game.get('summary')
        genre_set = game.get('genres')
        # Genre posses none to many tags which needs to be sorted.
        genre_list = []
        if genre_set:
            for type in genre_set:
                for i in type:
                    genre_list.append(type[i])
                    for i in genre_list:
                        genre_list = [x for x in genre_list if not isinstance(x, int)]
        else:
            pass
        # Group together by game
        if game.get('cover') != None:
            result = [name,cover_url.get('url'),storyline,summary,genre_list]
            # Add the game to the collection of all games found
            all_games[key] = result
            key += 1
        else: 
            result = [name,storyline,summary,genre_list]
            # Add the game to the collection of all games found
            all_games[key] = result
            key += 1

    return all_games

我错过了什么?

标签: pythondjangodictionaryimportnested

解决方案


推荐阅读